Add DataGridViewComboBoxCell to GridView c#

浪子不回头ぞ 提交于 2019-12-25 18:17:00

问题


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


回答1:


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



回答2:


DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.datasource = stringlist;

SampleGridView.Columns.Add(combo);

This is off the top of my head but should work.



来源:https://stackoverflow.com/questions/14468684/add-datagridviewcomboboxcell-to-gridview-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!