Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set selectedValue in DataGridViewComboBoxColumn

I have two tables: columns and tables (foreign key - Table_ID). I want to show columns in dataGridView with combobox. In combobox to be displayed related table (name tables) and selected item be value that set in columns.

List<columns> columns = DataLoader.GetColumns();
List<tables> tables = DataLoader.GetTables();

this.editingDataGridView.DataSource = columns; // my dataGridView
DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();

comboBoxColumn.DisplayMember = "Table_Name";
comboBoxColumn.ValueMember = "Table_ID";
comboBoxColumn.DataSource = tables;

//add combobox column in dataGrid
this.editingDataGridView.Columns.Add(comboBoxColumn);

//AND this i want set value
int index = this.editingDataGridView.Columns.IndexOf(comboBoxColumn);
for (int i = 0; i < columns.Count; i++)
{
   this.editingDataGridView.Rows[i].Cells[index].Value = columns[i].Table_ID;
}

After run, I get gridView with combobox column with dataSource, but without selected default value! enter image description here

like image 845
isxaker Avatar asked Mar 26 '26 21:03

isxaker


1 Answers

The key poin is

comboBoxColumn.DataPropertyName = "Table_ID";

Need to set DataPropertyName of dataGridComboBoxColumn

More this

like image 182
isxaker Avatar answered Mar 29 '26 11:03

isxaker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!