简体   繁体   中英

Add dropdown list to DataGrid columns and populate rows at runtime C#

My end goal is to populate a DataGrid with data retrieved from a database at runtime. My methodology is to pull all the columns from that database first and then pull the corresponding data for each column for each row as below:

    ReadOnlyCollection<Field> tablefields = {//has the fields populated from the database};
//adding all the columns to the data table    
         var datatable = new DataTable("mytable");
                            foreach (var field in tablefields)
                            { datatable.Columns.Add(field.Name, wFieldType); }
    
//Then add the rows
     while (cursor.MoveNext())
                                {
                                    var tableRow = datatable.NewRow();
    
                                    var row = cursor.Current;
                                    for(var i = 0; i < tablefields.Count; i++)//add the values for each field to the data table.
                                    {
                                        tableRow[i] = row[i] ?? DBNull.Value;
                                    }
                                    datatable.Rows.Add(tableRow);                                  
    
                                }     

             

My problem however is that some of the columns that I am pulling have to be in the form of a DataGridComboBoxColumn since they have preset values in a dropdown list in the database. The best case scenario would have been that as I pull the columns into my dataTable one by one, I would detect the ones that have preset values and make those oftype DataGridComboBoxColumn before adding them to the dataTable. I obviously can't do that because you cannot have a combobox on a dataTable. My next option is to use the dataGrid to load the columns and the data from the database at runtime. I am able to add the columns with a combobox as shown in the code below but I cannot figure out how to get my data from the database into the dataGrid containing the pre-loaded columns.

DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
cb.ItemsSource = new List(){to be populated later}
cb.Header = field.Name;                  
MyDataGrid.Columns.Add(cb);

The code above was used as a sample to test the DataGridComboBoxColumn and sure enough I was able to see the datagrid with the comboboxes. I just can't figure out how to get my database data into the table. I would appreciate any ideas. PS: I cannot use datagridview because of the api that I am working with so I am limited to datagrid.

You should set the SelectedItemBinding to a binding to a column in your DataTable that contains the value in the ItemsSource to be selected:

cb.SelectedItemBinding = new Binding("AColumnInYourDataTable");

If you set the ItemsSource to a List<T> where T is a complex type, you should instead set the SelectedValueBinding and SelectedValuePath and DisplayMemberPath properties:

cb.SelectedValueBinding = new Binding("AColumnInYourDataTable");
cb.SelectedValuePath = "NameOfThePropertyOfTThatHoldsTheValue";
cb.DisplayMemberPath = "NameOfAPropertyOfTThatHoldsTheDisplayName";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM