Search code examples
c#gridviewcombobox

Gridview combobox column hide items programmatically


I am trying to change the item collection programmatically in the Datagridview Combobox control.I have 3 items in Combobox which i get the datas from database;

[Item 1]

[Item 2]

[Item 3]

What i wanted to do is ;

If the combobox value is [Item 1] as a default, i can only select the [Item 2].I dont want to see the [Item 3] in the list.

Can anyone help me about this?


Solution

  • I solved via using CellBeginEdit event of datagrid and used cell.datasource,

    private void dGV_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            DataGridViewRow row = dGV.Rows[e.RowIndex];
    
              if (item_type.ToString() == "item1")
                {
                    try
                    {
                         DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(row.Cells[3]);
                         cell.DataSource = new string[] { "Item1","Item2"};
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
        }