Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

Add DataGridViewComboBoxCell to GridView c#

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...