views:

3221

answers:

3

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?

+2  A: 

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);
    }
}
WaterBoy
This code worked great for a project that I'm currently working on.
Chris Miller
A: 

Hi

Can you tell me where should I call this method?

Cheers

WS
A: 
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == DataGridViewComboBoxColumnNumber)
        {
            setCellComboBoxItems(myDataGridView, e.RowIndex, e.ColumnIndex, someObj);
        }
    }
Steve