Reputation: 11
I have a datagridview on a windows form. It has clolumn [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?
Upvotes: 1
Views: 13087
Reputation: 64487
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.
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.
Some helpful links:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
Upvotes: 5