Answer the question
In order to leave comments, you need to log in
How to set value in DataGridViewComboBoxColumn?
I want to display information from a table in a DataGridView, and in one of the columns, pull up a related table and display it as a DropDown sheet.
List<columns> columns = DataLoader.GetColumns();
List<tables> tables = DataLoader.GetTables();
editingDataGridView.DataSource = columns; // my dataGridView
DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn(); //my combobox column
comboBoxColumn.DisplayMember = "Table_Name";
comboBoxColumn.ValueMember = "Table_ID";
comboBoxColumn.DataSource = tables;
//add combobox column in dataGrid
this.editingDataGridView.Columns.Add(comboBoxColumn);
int index = this.editingDataGridView.Columns.IndexOf(comboBoxColumn);
//AND this i want set value
for (int i = 0; i < columns.Count; i++)
{
//do not work; not set value from columns
this.editingDataGridView.Rows[i].Cells[index].Value = columns[i].Table_ID;
}
Answer the question
In order to leave comments, you need to log in
That's where the key was
comboBoxColumn.DataPropertyName = "Table_ID";
int index = this.editingDataGridView.Columns.IndexOf(comboBoxColumn);
int index = comboBoxColumn.Index;
(this.editingDataGridView.Rows[i].Cells[index] as DataGridViewComboBoxCell).Value = tables.FirstOrDefault(c =>c.Table_ID == columns[i].Table_ID);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question