populate combobox with names and ids
My database table contains two fields, id and name
What is the way to populate the combobox with the projectnames and have the id of each projectname assigned aswell.
This is required so that if for example, from a list of names in a listbox, a name is selected, then I want the projectname to be selected in the project combobox.
The table returns the name and the projectID
I can populate the combobox with the projectnames from the project table but not sure how to assign the IDs to them.
<pre><ComboBox Name="cboProjects" SelectedIndex="{Binding Path=ProjectID}"
/></pre>
Thanks
Re: populate combobox with names and ids
Are you taking full advantage of WPF's DataBinding? By which I mean, are you binding your combobox to the DataTable rather than adding the Name from each row to the Items collection programatically? If you bind the DataTable then each item is the whole row, rather than just the Name. Therefore you have both the Name and the Id available. Use the DisplayMemberPath to specify that you want the Name to be shown.
Are you aware of the IsSynchronisedWithCurrentItem property on collection controls such as the ListBox and ComboBox?
If you have bound the two controls to the DataTable (well, you can't bind directly to a dataTable, you need to bind to a DataView of the DataTable) then they will be both pointing to the same view of the data. This "collection view" handles (among other things like sorting and filtering) the notion of "currency" which is nothing to do with money, but rather the "current item".
Setting the IsSynchronisedWithCurrentItem property on the controls to True makes the controls synchronise their SelectedItem with the underlying collection view. And since (unless you specify otherwise) both controls will use the same default view, any change in the selected item of one control will ripple back to the collection view and then on to the other control. So, if you do something like this:
In the code-behind of your form:
csharp Code:
public MainWindow()
{
InitializeComponent();
DataTable dataTable = GetDataTable();
DataContext = new DataView(dataTable);
}
private DataTable GetDataTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Id");
dataTable.Columns.Add("Name");
for (int i = 0; i < 10; i++)
{
DataRow row = dataTable.NewRow();
row["Id"] = i;
row["Name"] = string.Format("Project {0}", i);
dataTable.Rows.Add(row);
}
return dataTable;
}
Then in your Xaml, do the following:
Code:
<ListBox Height="200"
ItemsSource="{Binding}"
DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True"/>
<ComboBox ItemsSource="{Binding}"
DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True"/>
You'll see that the two controls' SelectedItem are synchronised.
Re: populate combobox with names and ids
I have now included the real sample of what I am trying to do...
You see that first I am retrieving data from a stored procedure which then correctly gets populated into a listcollection called grades.
First problem is that the combo box does not get populated.
Note that I can see there is correct data in cboGrades.ItemsSource
Do you see what I am doing wrong please?
Thanks
public static List<clsStudentDetails> GetGrades()
{ DbCommand comm = clsGenericDataAccess.CreateCommand(); comm.CommandText = "uspGetGrades"; DataTable table = new DataTable(); table = clsGenericDataAccess.ExecuteSelectCommand(comm); List<clsStudentDetails> grades = new List<clsStudentDetails>(); for (int i = 0; i < table.Rows.Count; i++) { clsStudentDetails details = new clsStudentDetails(); details.GradeID = int.Parse(table.Rows[i]["GradeID"].ToString()); details.GradeName = table.Rows[i]["GradeName"].ToString(); grades.Add(details); } return grades; }---------------------private void PopulateGrades() { List<clsStudentDetails> studentDetails = new List<clsStudentDetails>(); cboGrades.ItemsSource = clsStudentAccess.GetGrades(); }---------------------<ComboBox Name="cboGrades" DisplayMemberPath="GradeName" SelectedValuePath="GradeID" Width="208" />