I have a Session table. Within the Session table I have a field - SessionTypeID - that references the SessionType table (foreign key). I have a combobox that I have created with SessionTypeName entries from the SessionType file. When a Session is selected I want to match the SessionTypeID from the Session file, with the entries in the SessionType combobox, and make that the selected (default) index. I assumed I would iterate through the combobox items and find the matching entry, but it's proving more difficult than I anticipated. I'm using WPF, C# and .NET 4.6 All the examples I've found don't seem to work with 4.6.
For example:
for (int i = 0; i < comboBoxSessionDataSessionType.Items.Count; ++i)
{
MessageBox.Show(comboBoxSessionDataSessionType.Items[i].ToString());
}
shows the following:
'Systems.Windows.Controls.ComboBoxItem: 120 Film'
'120 Film'
is the value I'm looking for.
Any ideas on how to accomplish this?
Here is the code for getting the Session table data:
string sql = @"SELECT * FROM Session WHERE SessionID = '" + id + "'";
SQLiteCommand com = new SQLiteCommand(sql, conn);
System.Data.DataSet ds = new System.Data.DataSet();
SQLiteDataAdapter ad = new SQLiteDataAdapter(sql, conn);
ad.Fill(ds);
GridSessionData.DataContext = ds.Tables[0].DefaultView;
Here is the button XAML code:
<combobox x:name="comboBoxSessionDataSessionType" grid.column="1" grid.row="2" style="{DynamicResource ComboboxData}" itemssource="{Binding Path=SessionTypeID, Mode=TwoWay}" datacontext="{Binding SessionTypeName}" selecteditem="{Binding Path=SessionTypeID}" selectedvalue="{Binding Path=SessionTypeID}" tag="{Binding Path=SessionTypeID, Mode=TwoWay}" xmlns:x="#unknown" />
string sql = @"SELECT * FROM Session WHERE SessionID = '" + id + "'";
SQLiteCommand com = new SQLiteCommand(sql, conn);
System.Data.DataSet ds = new System.Data.DataSet();
SQLiteDataAdapter ad = new SQLiteDataAdapter(sql, conn);
ad.Fill(ds);
GridSessionData.DataContext = ds.Tables[0].DefaultView;
Here is the button XAML code:
<combobox x:name="comboBoxSessionDataSessionType" grid.column="1" grid.row="2" style="{DynamicResource ComboboxData}" itemssource="{Binding Path=SessionTypeID, Mode=TwoWay}" datacontext="{Binding SessionTypeName}" selecteditem="{Binding Path=SessionTypeID}" selectedvalue="{Binding Path=SessionTypeID}" tag="{Binding Path=SessionTypeID, Mode=TwoWay}" xmlns:x="#unknown">
Mika, here is how I filled the combobox:
string sql = @"SELECT SessionTypeID, SessionTypeName FROM SessionType WHERE Active = 1 ORDER BY SessionTypeName";
SQLiteCommand com = new SQLiteCommand(sql, conn);
System.Data.DataSet ds = new System.Data.DataSet();
SQLiteDataReader dr = com.ExecuteReader();
List<ComboBoxItem> list = new List<ComboBoxItem>();
while (dr.Read())
{
ComboBoxItem item = new ComboBoxItem();
item.Content = dr.GetValue(1).ToString();
item.Tag = dr.GetValue(0).ToString();
list.Add(item);
}
ComboBoxItem itm = new ComboBoxItem();
itm.Content = eesConstants.EESADDEDIT;
itm.Tag = "";
list.Add(itm);
(eesConstants.EESADDEDIT);
comboBoxSessionDataSessionType.ItemsSource = list;
When I retrieve the Session entry, I know what the SessionTypeID is:
ds.Tables[0].Rows[0]["SessionTypeID"].ToString()
Now that I have the SessionTypeID, how do I match it up with the correct entry in the SessionType combobox?
The combobox ItemSource is connected to the dataset where I'm getting the Session Data from:
<ComboBox x:Name="comboBoxSessionDataSessionType" Grid.Column="1" Grid.Row="2" Style="{DynamicResource ComboboxData}" ItemsSource="{Binding Path=SessionTypeID, Mode=TwoWay