简体   繁体   English

在数据网格视图中添加组合框,下拉框或列表框

[英]Add combo box, drop down or list box in data gridview

Do you know how i could add a combo box, drop down or list box in a specific cell in a data grid view. 您知道我如何在数据网格视图的特定单元格中添加组合框,下拉框或列表框。 i have tried with this code but it doesn't work-- 我已经尝试过使用此代码,但无法正常工作-

DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Rows[1].Cells[1] = c;

Try: 尝试:

DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C);
RunTimeCreatedDataGridView.Columns.Add(c);

i found the answer-- 我找到了答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM