Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewComboBoxColumn adding different items to each row .

I am building a table using the DataGridView where a user can select items from a dropdown in each cell. To simplify the problem, lets say i have 1 column. I am using the DataGridViewComboBoxColumn in the designer. I am trying to support having each row in that column have a different list of items to choose from.

Is this possible?

like image 251
leora Avatar asked Sep 18 '08 11:09

leora


1 Answers

Yes. This can be done using the DataGridViewComboBoxCell.

Here is an example method to add the items to just one cell, rather than the whole column.

private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, object[] itemsToAdd)
{
    DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell) dataGrid.Rows[rowIndex].Cells[colIndex];
    // You might pass a boolean to determine whether to clear or not.
    dgvcbc.Items.Clear();
    foreach (object itemToAdd in itemsToAdd)
    {
        dgvcbc.Items.Add(itemToAdd);
    }
}
like image 162
WaterBoy Avatar answered Oct 06 '22 09:10

WaterBoy