DataGridViewComboBoxColumn向每一行添加不同的项目.

leo*_*ora 12 c# datagridview winforms

我正在使用DataGridView构建一个表,用户可以从每个单元格的下拉列表中选择项目.为了简化问题,我想说我有1列.我在设计器中使用DataGridViewComboBoxColumn.我试图支持让该列中的每一行都有不同的项目列表供您选择.

这可能吗?

Wat*_*Boy 17

是.这可以使用DataGridViewComboBoxCell完成.

这是一个将项目添加到一个单元格而不是整个列的示例方法.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)