There are several ways to go about this task. The simplest way involves adding a single combobox to the DataGrid.Controls, and then selectively displaying it as needed when a combobox cell becomes the currentcell. All the work is done in a few event handlers and no overrides or derived classes are necessary. This technique is discussed in Microsoft KB article Q323167.
The other techniques require you to derive a columnstyle. Attached is a dropdown combobox sample (C#, VB) that shows how you can use a combobox in a datagrid. This implementation differs from other available columnstyle samples (gotdotnet.com and C# Corner )
in that it derives from DataGridTextBoxColumn. These other samples
derive directly from DataGridColumnStyle, and thus have to add functionality that already exists
in DataGridTextBoxColumn.
This derived DataGridTextBoxColumn does not implement a databound combobox where you can set its DataSource, DisplayMember, and ValueMember to bind the combobox to a foreign table. If you need such a combobox, there is another sample link referenced at the end of this FAQ that does implement such a databound combobox.
This sample just attempts to replace the TextBox member of DataGridTextBoxColumn with a standard
ComboBox member. Only two overrides need to be handled along with 2 events to get a functional
implementation.
Here are the notes from the code that list the 3 steps to add a combobox to your datagrid.
|