Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
131 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...