views:

17

answers:

1

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?

A: 

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 dropdown, that's available out of the box.

To populate the ComboBox 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

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e8e2d9eb-fe39-42ab-9a18-2194dac54675/

Adam
If i set the columnType to Combobox column, only the current value for the department is displayed in combobox, How can i show all the possible option. I have a list of departments in a table.
Pafio
I've edited my answer.
Adam