Reputation: 3177
Seems like it's so simple but for some reason working on it last 4 hours with no result. I have simple Gridview with two columns and few rows already filled, now I want to add ComboBoxCell to the next row.
I'll show my logic and maybe you'll show me my error:
SampleGridView.Rows.Add("test1", "test1");
SampleGridView.Rows.Add("test2", "test2");
SampleGridView.Rows.Add("test3", "test3");
works fine for three rows now I am inserting my ComboBox:
DataGridViewRow RowSample = new DataGridViewRow();
DataGridViewComboBoxCell CellSample = new DataGridViewComboBoxCell();
CellSample.DataSource = StringList; // list of the items that I want to insert in ComboBox
RowSample.Cells.Add(CellSample);
SampleGridView.Rows.Add(RowSample);
Any ideas? Thank you
Upvotes: 0
Views: 10108
Reputation: 3177
figured it out :) I was trying to insert ComboBox to the second cell of the row, and it didn't work. I was trying to do it like this:
SampleGridView.Rows.Add("CellText", RowSample);
but it actually have to be like this
DataGridViewRow RowSample = new DataGridViewRow();
DataGridViewComboBoxCell CellSample = new DataGridViewComboBoxCell();
CellSample.DataSource = StringList; // list of the string items that I want to insert in ComboBox
CellSample.Value = StringList[0]; // default value for the ComboBox
DataGridViewCell cell = new DataGridViewTextBoxCell();
cell.Value = "CellText"; // creating the text cell
RowSample.Cells.Add(cell);
RowSample.Cells.Add(CellSample);
SampleGridView.Rows.Add(RowSample);
Upvotes: 2
Reputation: 8630
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.datasource = stringlist;
SampleGridView.Columns.Add(combo);
This is off the top of my head but should work.
Upvotes: 1