简体   繁体   中英

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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