Results 1 to 3 of 3

Thread: populate combobox with names and ids

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    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>&lt;ComboBox Name="cboProjects" SelectedIndex="{Binding Path=ProjectID}"
    /&gt;</pre>

    Thanks

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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:
    1. public MainWindow()
    2. {
    3.     InitializeComponent();
    4.  
    5.     DataTable dataTable = GetDataTable();
    6.     DataContext = new DataView(dataTable);
    7. }
    8.  
    9. private DataTable GetDataTable()
    10. {
    11.     DataTable dataTable = new DataTable();
    12.  
    13.     dataTable.Columns.Add("Id");
    14.     dataTable.Columns.Add("Name");
    15.  
    16.     for (int i = 0; i < 10; i++)
    17.     {
    18.         DataRow row = dataTable.NewRow();
    19.         row["Id"] = i;
    20.         row["Name"] = string.Format("Project {0}", i);
    21.         dataTable.Rows.Add(row);
    22.     }
    23.     return dataTable;
    24. }

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    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" />

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width