I’ve got a datatable and one column is an integer ID foreign key to another database table.
I’ve got a datagridview and i’d like to use a combobox column to allow user to change value. But instead of using the integers, it would be great to use the names.
I’ve tried creating a simple struct with public members int ID and string Name; a dictionary and looked into enums (however values not known at compile time) but not got anything to work yet.
I was able to populate the combobox with struct values, but not able to programmatically set the selected item/index; ie, if ID “5” is in the datatable, set the combo box selected item to the struct that has an ID of 5.
So to be clear i’m wanting:
gridview datasource's fk ID's
1
2
3
Foreign Key table:
ID Name
1 Name 1
2 Name 2
3 Name 3
Datagridviewcombobox column should be loaded with three items; should display as “Name 1, Name 2, Name 3”. Based on the gridview datasource’s FK id, the selected item for each should match.
You can set the
DataGridViewComboBoxColumn.DataSourceproperty, and then use theValueMemberandDisplayMemberproperties to determine what is shown in theComboBox. Easiest is probably to load your FK values intoDataTableand use that as the data source. For your example:I am not sure how you are binding your
DataGridViewto your initialDataTable, but you can associate theDataGridViewComboBoxcolumn with a specific column in your originalDataTableusingDataPropertyName: