DataGridViewComboBoxColumn 向每一行添加不同的项目. [英] DataGridViewComboBoxColumn adding different items to each row .

查看:25
本文介绍了DataGridViewComboBoxColumn 向每一行添加不同的项目.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这可能吗?

推荐答案

是的.这可以使用 DataGridViewComboBoxCell 来完成.

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);
    }
}

这篇关于DataGridViewComboBoxColumn 向每一行添加不同的项目.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆