3

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

isxaker
  • 8,446
  • 12
  • 60
  • 87
  • The selected value depends on the current row of the underlying datasource. What is the default value you want? – King King Aug 19 '13 at 13:26
  • That containde in List columns on field Table_ID. I do it after adding combobxColumn in loop(for each comboboCell i set Value from columns list) – isxaker Aug 19 '13 at 13:30
  • @KingKing example, in object columns i have field Table_id = 1, after loading datasource into combobox i want that selected value equal 1! – isxaker Aug 19 '13 at 14:06

2 Answers2

4

The key poin is

comboBoxColumn.DataPropertyName = "Table_ID";

Need to set DataPropertyName of dataGridComboBoxColumn

More this

isxaker
  • 8,446
  • 12
  • 60
  • 87
0

In vb.net, I found this solution

Dim cbx As DataGridViewComboBoxCell = dgvEstudios.Rows(x).Cells(1)
cbx.Value = Trim("String value")
Prune
  • 76,765
  • 14
  • 60
  • 81
Jean Paul Beard
  • 337
  • 3
  • 3