Search code examples
c#wpfdatagridviewwpfdatagriddatagridcomboboxcolumn

how do I dynamically update the selection of comboxboxcolumn in datagrid?


I'm trying to display two columns in a datatable(dt) into a datagrid. And add another column which is a comboxcolumn so that I can assign sectors to each rows. The problem is that the selection of comboboxes cannot be dynamically updated. when I click something else, the selection is gone. Is there any way to fix this?

   dg_display.DataContext = dt.DefaultView;
   dg_display.Columns[0].Header = "Symbol";
   dg_display.Columns[1].Header = "Company name";          
   DataGridComboBoxColumn columnComboBox = new DataGridComboBoxColumn();
   string[] sectorarray = new[]
                                    {
                                       "Consumer Discretionary", "Consumer     Staples", "Energy", "Financial",
                                       "Financials", "Health Care", "Industrials", "Information Technology",
                                       "Materials", "Other", "Technology","Telecommunication Services", "Utilities"
                                   };
        columnComboBox.ItemsSource = sectorarray;
        columnComboBox.IsReadOnly = false;
                    dg_display.Columns.Add(columnComboBox);
        dg_display.Columns[2].Header = "Sector";

Solution

  • You're not binding the SelectedItem in the ComboBox

    Set either SelectedItemBinding or SelectedValueBinding