I have an event for a ComboBox, "SelectionChange".
Here's what I'm trying to do:
- I have two ComboBoxes
- The second ComboBox will display items depending on the selected item on the first Box
- ComboBox2 should react as soon as an item on ComboBox1 is selected
My problem is when I'm trying to get the SelectedIndex.
When I use ComboBox1.Text after confirming the SelectedIndex, it returns null so the ComboBox2 doesn't react.
I tried placing a button to force the event and it did work. It seems that the SelectedIndex doesn't change until you release focus.
Here's a snippet of the code:
if (cb_subj.SelectedIndex == ctr)
{
cb_section.Items.Clear();
if (connectToDB.openConnection() == true)
{
MySqlDataAdapter comboBoxItems_seclist = new MySqlDataAdapter();
MySqlCommand query = new MySqlCommand(@"SELECT section_code FROM sections
WHERE subject = @subj", connectToDB.connection);
query.Parameters.AddWithValue("@subj", cb_subj.Text);
comboBoxItems_seclist.SelectCommand = query;
System.Data.DataTable classlist = new System.Data.DataTable();
comboBoxItems_seclist.Fill(classlist);
foreach (System.Data.DataRow row in classlist.Rows)
{
string rows = string.Format("{0}", row.ItemArray[0]);
cb_section.Items.Add(rows);
}
}
break;
}
Here's the XAML of the two CB:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="166,12,0,0" Name="cbox_year" VerticalAlignment="Top" Width="120" SelectionChanged="cbox_year_SelectionChanged">
<ComboBoxItem Content="1st Year / 1st Sem" />
<ComboBoxItem Content="1st Year / 2nd Sem" />
<ComboBoxItem Content="2nd Year / 1st Sem" />
<ComboBoxItem Content="2nd Year / 2nd Sem" />
<ComboBoxItem Content="3rd Year / 1st Sem" />
<ComboBoxItem Content="3rd Year / 2nd Sem" />
<ComboBoxItem Content="4th Year / 1st Sem" />
<ComboBoxItem Content="4th Year / 2nd Sem" />
</ComboBox>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="166,41,0,0" Name="cb_subj" VerticalAlignment="Top" Width="120" SelectionChanged="cb_subj_SelectionChanged" />