获取DataGridViewComboBoxCell的显示值和基础值

 

获取DataGridView 的 DataGridViewComboBoxCell单元格的显示值和基础值

 

DataGridViewCell cell = dataGridView.Rows[r].Cells[c];//得到单元格
if (cell is DataGridViewComboBoxCell)
{
    DataGridViewComboBoxCell cb = cell as DataGridViewComboBoxCell;

    string showValue = ((System.Data.DataRowView)cb.Items[n])[cb.DisplayMember].ToString();//显示值

    string baseValue = ((System.Data.DataRowView)cb.Items[n])[cb.ValueMember].ToString();//基础值

    //cell.Value是下拉框的索引值
}

在C#中,DataGridViewComboBoxCellDataGridView控件中的一个单元格类型,用于显示下拉列表,允许用户从下拉列表中选择一个。要给DataGridViewComboBoxCell赋初,可以按照以下步骤操作: 1. 首先,你需要创建一个下拉列表数据源。这通常是一个字符串数组或者列表等集合。 ```csharp string[] items = new string[] { "选项1", "选项2", "选项3" }; ``` 2. 接着,在DataGridView的适当位置(例如在Form的构造函数或者窗体加载事件中)为DataGridViewComboBoxCell设置数据源,并指定初始选中的项。 ```csharp // 假设你已经添加了DataGridViewComboBoxCellDataGridView中,并知道该cell的名称或索引 DataGridViewComboBoxCell comboBoxCell = dataGridView1.Rows[0].Cells["comboboxColumnName"] as DataGridViewComboBoxCell; comboBoxCell.Items.AddRange(items); // 设置下拉列表的数据源 comboBoxCell.Value = items[1]; // 设置下拉列表的默认选中,这里选中"选项2" ``` 3. 如果DataGridViewComboBoxCell已经与某个列关联了,那么可以通过该列来设置。 ```csharp DataGridViewComboBoxColumn comboBoxColumn = dataGridView1.Columns["comboboxColumnName"] as DataGridViewComboBoxColumn; comboBoxColumn.Items.AddRange(items); comboBoxColumn.HeaderText = "下拉列"; // 设置列头标题 ``` 4. 如果是在数据绑定的情况下,你还可以通过数据源的设置来实现。 ```csharp comboBoxColumn.DataSource = items; comboBoxColumn.DisplayMember = "显示名称"; // 假设items是一个包含显示名称属性的对象数组 comboBoxColumn.ValueMember = "名称"; // 同上 // 设置默认选中的项需要根据具体的绑定方式来定,可能需要额外的代码 ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值