windows windows窗体Datagridview控件中的ComboBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3177717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ComboBox in windows forms Datagridview control
提问by Pafio
I have a datagridview on a windows form. It has clolumn [EmployeeNumber, EmployeeName and EmployeeDepartment
].
我在 Windows 窗体上有一个 datagridview。它有克隆 [ EmployeeNumber, EmployeeName and EmployeeDepartment
]。
I want to be able to edit values directly in the grid but i want EmployeeDepartment to be a combobox column, such that on cell edit, a combobox shows up with available options for the employee department. Has any one been able to implement such functionality?
我希望能够直接在网格中编辑值,但我希望 EmployeeDepartment 是一个组合框列,这样在单元格编辑时,组合框会显示员工部门的可用选项。有没有人能够实现这样的功能?
回答by Adam Houldsworth
The DataGridView
actually has a DataGridViewComboBoxColumn
. If you go to the column designer, and add a new column you'll notice you can select one of a few types. It behaves exactly as you have described - on edit, it appears. The DataGridView
uses things called editing controls, and has various events for swapping the control into the cell on edit.
该DataGridView
实际上有一个DataGridViewComboBoxColumn
。如果您转到列设计器,并添加一个新列,您会注意到您可以选择几种类型之一。它的行为与您描述的完全一样 - 在编辑时,它会出现。在DataGridView
使用东西叫做编辑控件,并具有用于交换控制插入编辑细胞的各种活动。
Either way, if all you want is a drop-down, that's available out of the box.
无论哪种方式,如果您只想要一个下拉菜单,那么它是开箱即用的。
To populate the combo box with items, in the column editor you will notice that it has an Items
property for manually setting items, or you can use the data binding properties: DataSource
, DisplayMember
, ValueMember
. Data-binding has issues when an expected value is not in the items collection, you will get a lot of cell errors; but they can be avoided.
要用项目填充组合框,在列编辑器中,您会注意到它有一个Items
用于手动设置项目的属性,或者您可以使用数据绑定属性:DataSource
、DisplayMember
、ValueMember
。当预期值不在项目集合中时,数据绑定会出现问题,您将收到很多单元格错误;但它们是可以避免的。
Some helpful links:
一些有用的链接:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx