0

I have a DataGridView which has a ComboBoxColumn I added in the form designer. In my code I want to add rows which each have individual ComboBoxCell's. This is due to each row having different values in the ComboBoxes.

I have tried adding DataGridViewComboBoxCell items as shown below and also by creating a DataTable and binding it to the DataGridViewComboBoxCell. When I run the program, I can see the rows and ComboBoxes, but when I attempt to click the boxes, no items are displayed.

        DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
        DataGridViewComboBoxCell cell2 = new DataGridViewComboBoxCell();

        cell.Items.Add("Item1");
        cell.Items.Add("Item2");
        cell.Items.Add("Item3");

        cell2.Items.Add("Item4");
        cell2.Items.Add("Item5");
        cell2.Items.Add("Item6");


        DataGridViewRow row = new DataGridViewRow();
        DataGridViewRow row1 = new DataGridViewRow();

        row.Cells.Add(cell);
        row1.Cells.Add(cell2);

        dataGridView1.Rows.Clear();
        dataGridView1.Rows.Add(row);
        dataGridView1.Rows.Add(row1);
        dataGridView1.Refresh();

I can't really seem to find a solution in other posts. Can anyone help get the items to display?

EDIT: The DataGridView has been set to disable editing, which I have discovered stops me from viewing the ComboBox items. In the real program there are other columns, and it is meant to be read only which is why I disabled editing. I would still like the user to be able to click and view the ComboBox items.

Duane
  • 1,980
  • 4
  • 17
  • 27
  • `I attempt to click the boxes, no items are displayed`. I tried your code. if click the arrow then the combo box displayed. So Explain which items are not displayed – Sathish Aug 20 '14 at 07:21
  • @Sathish When I click the arrows, no items are displayed. The DataGridView is nested in a TabControl, but I don't assume that should make any difference. – Duane Aug 20 '14 at 07:22

1 Answers1

0

Make sure you have selected DataGridViewComboboxColumn from the ColumnType for that column.

If you have not added the column then you can add like this.

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.Name ="cmbItem";
grd.Columns.Add(column);

if you have already added that column then you can add items directly from taking reference of combobox cell.

int rowIndex  = grd.Rows.Add();
DataGridViewComboBoxCell oCell = (DataGridViewComboBoxCell)grd.Rows[rowIndex].Cells["cmbItem"];
oCell.Items.Add("Item1");
oCell.Items.Add("Item2");
oCell.Items.Add("Item3");

rowIndex = grd.Rows.Add();
DataGridViewComboBoxCell oCell = (DataGridViewComboBoxCell)grd.Rows[rowIndex].Cells["cmbItem"];
oCell.Items.Add("Item4");
oCell.Items.Add("Item5");
oCell.Items.Add("Item6");
Shell
  • 6,818
  • 11
  • 39
  • 70