且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# Datagridview:获取组合框列中的选定项

更新时间:2022-11-07 19:30:32

DataGridView 中的 Control 不是 ComboBox,它是 DataGridViewComboBox 并且具有不同的属性和方法.来自 MSDN一个>

The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN

与 ComboBox 控件不同,DataGridViewComboBoxCell 没有 SelectedIndex 和 SelectedValue 属性.相反,从下拉列表中选择一个值会设置单元格的 Value 属性.

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

但是,您提到 Cell.Value 对您来说是 null.根据以下文章(如何:访问 Windows 窗体 DataGridViewComboBoxCell 下拉列表中的对象).

However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).

您必须将 DataGridViewComboBoxColumn.ValueMember 或 DataGridViewComboBoxCell.ValueMember 属性设置为业务对象上的属性名称.当用户进行选择时,业务对象的指示属性设置单元格值属性.

You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.