在数据网格视图中添加组合框、下拉列表框或列表框
Add combo box, drop down or list box in data gridview
您知道如何在数据网格视图的特定单元格中添加组合框、下拉列表框或列表框吗?我已尝试使用此代码,但它不起作用--
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Rows[1].Cells[1] = c;
尝试:
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Columns.Add(c);
我找到了答案--
//create columns as textbox columns(or whatever you want)
DataGridViewTextBoxColumn dgc = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn dgc1 = new DataGridViewTextBoxColumn();
//add the colums to the table
dataGridView1.Columns.Add(dgc);
dataGridView1.Columns.Add(dgc1);
//set header text here if you wish
//create dgv combobox cells and add items to the collection
DataGridViewComboBoxCell cbc = new DataGridViewComboBoxCell();
cbc.Items.Add("item 1");
cbc.Items.Add("item 2");
DataGridViewComboBoxCell cbc1 = new DataGridViewComboBoxCell();
cbc1.Items.Add("item 1");
cbc1.Items.Add("item 2");
//create 2 rows here so that you can insert future columns without having them go above your comboboxes
dataGridView1.Rows.Add(2);
//set row one cells to combobox cells
dataGridView1.Rows[0].Cells[0] = cbc;
dataGridView1.Rows[0].Cells[1] = cbc1;
您知道如何在数据网格视图的特定单元格中添加组合框、下拉列表框或列表框吗?我已尝试使用此代码,但它不起作用--
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Rows[1].Cells[1] = c;
尝试:
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Columns.Add(c);
我找到了答案--
//create columns as textbox columns(or whatever you want)
DataGridViewTextBoxColumn dgc = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn dgc1 = new DataGridViewTextBoxColumn();
//add the colums to the table
dataGridView1.Columns.Add(dgc);
dataGridView1.Columns.Add(dgc1);
//set header text here if you wish
//create dgv combobox cells and add items to the collection
DataGridViewComboBoxCell cbc = new DataGridViewComboBoxCell();
cbc.Items.Add("item 1");
cbc.Items.Add("item 2");
DataGridViewComboBoxCell cbc1 = new DataGridViewComboBoxCell();
cbc1.Items.Add("item 1");
cbc1.Items.Add("item 2");
//create 2 rows here so that you can insert future columns without having them go above your comboboxes
dataGridView1.Rows.Add(2);
//set row one cells to combobox cells
dataGridView1.Rows[0].Cells[0] = cbc;
dataGridView1.Rows[0].Cells[1] = cbc1;