Click here to Skip to main content
Click here to Skip to main content
Go to top

ComboBox in a DataGrid

, 13 Sep 2006
Rate this:
Please Sign up or sign in to vote.
How to embed a ComboBox (DropDownList) in a DataGrid.


I needed a ComboBox in my DataGrid. After looking around on the web, I found many examples, but none of them worked for me.

With inspiration from Alastair Stells' article here on The Code Project and whatever else I found on the Internet, I have made the following DataGridComboBoxColumn class.

Why did the other examples not work

All the other examples populate the ComboBox with a DataView, but I need to (want to be able to) populate my ComboBox with an IList (ArrayList) instead of a DataView.

columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
columnComboBox.comboBox.DisplayMember = "Name";
columnComboBox.comboBox.ValueMember = "GUID";

And MyDataClass.GetArray() returns MyDataClass[], and has two properties named Name and GUID.

The other examples expect columnComboBox.comboBox.DataSource to be a DataView, and it being an ArrayList generates exceptions.

I use the ComboBox to fetch display text

Since you don't know the type of columnComboBox.comboBox.DataSource, you can't use that to translate between the underlying data and what to display in the DataGrid.

Instead, I use the ComboBox itself, by overriding the ComboBox and implementing this method.

public string GetDisplayText(object value) {
   // Get the text.
   string text   = string.Empty;
   int  memIndex  = -1;
   try {
      memIndex     = base.SelectedIndex;
      base.SelectedValue = value.ToString();
      text      = base.SelectedItem.ToString();
      base.SelectedIndex = memIndex;
   } catch {
     return GetValueText(0);
   } finally {

   return text;
} // GetDisplayText

What I do is simple. I select the item which displays the text I want, get the text, and then reselect the original item. By doing it this way, it doesn't matter what data source is used.

Because I use the ComboBox itself to fetch the display text, the ComboBox must be populated before the DataGrid is drawn.

Alastair Stells noted about this in his article:

Another issue which arose was an eye-opener! I discovered the ComboBox does not get populated until the ComboBox.Visible property is set for the first time.

This means that the ComboBox can't be used to fetch the initial display text, because it is not visible when the DataGrid is first shown (painted).

I used a normal ComboBox to illustrate the problem and the solution.

ComboBox comboBox = new ComboBox();
comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
comboBox.DisplayMember = "Name"
comboBox.ValueMember = "GUID"
MessageBox.Show(comboBox.Items.Count.ToString()); // THIS IS ALWAYS 0!

I learned that it didn't help to show the ComboBox, but instead I had to set its parent - which internally commits the data from the DataSource to the Items collection.

ComboBox comboBox = new ComboBox();
comboBox.Parent = this; // this is a Form instance in my case.
comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
comboBox.DisplayMember = "Name"
comboBox.ValueMember = "GUID"
// THIS IS MyDataClass.GetArray().Count

What else about my DataGridComboBoxColumn

The source code is straightforward. First, I inherited DataGridTextBoxColumn, but my class then evolved into inheriting DataGridColumnStyle. This meant that I had to implement the Paint methods, but at this point, I had some examples of that as well. I like the idea of not having an invisible TextBox behind it all.

How to use

Sadly, I don't know how to "register" my DataGridComboBoxColumn with the GridColumnStyles, enabling me to design the DataGrid columns in the designer. This code does it manually:

// Add three MyDataClass objects, to the DataGridComboBox.
// This is the choices which will apear in the ComboBox in the DataGrid.
// You can see in the source that the MyDataClass doubles
// as a static collection, where the new MyDataClass objects
// automatically is added.
// All the MyDataClass objects can be retreived in an array
// with the static method: MyDataClass.GetArray().
if (MyDataClass.GetArray().Length == 0) {
    new MyDataClass("Denmark");
    new MyDataClass("Faroe Islands (DK)");
    new MyDataClass("Finland");
    new MyDataClass("Greenland (DK)");
    new MyDataClass("Iceland");
    new MyDataClass("Norway");
    new MyDataClass("Sweden");

// I don't have a database here, so I make my
// own DataTable with two columns and finally
// populate it with some test rows.
DataTable table = new DataTable("TableOne");

DataColumn column = table.Columns.Add();
column.ColumnName = "country";
// Realy a GUID from the DataGridComboBox.
column.DataType = Type.GetType("System.Guid");

column = table.Columns.Add();
column.ColumnName = "notes";
column.DataType = Type.GetType("System.String");

table.Rows.Add(new object[] {MyDataClass.GetArray()[0].GUID, 
                             "Population 5.368.854"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[1].GUID, 
                             "Population 46.011"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[2].GUID, 
                             "Population 5.183.545"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[3].GUID, 
                             "Population 56.376"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[4].GUID, 
                             "Population 279.384"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[5].GUID, 
                             "Population 4.525.116"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[6].GUID, 
                             "Population 8.876.744"});

// Create a DataGridTableStyle object.
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataGridTextBoxColumn columnTextBox;
DataGridComboBoxColumn columnComboBox;
tableStyle.RowHeadersVisible = true;
tableStyle.RowHeaderWidth = 20;

// Add customized columns.
// Column "notes", which is a simple text box.
columnTextBox = new DataGridTextBoxColumn();
columnTextBox.MappingName = "notes";
columnTextBox.HeaderText = "Country notes";
columnTextBox.Width = 200;

// Column "country", which is the ComboBox.
columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.Parent = this; // Commit dataset.
columnComboBox.comboBox.DataSource = 
               new ArrayList(MyDataClass.GetArray());
columnComboBox.comboBox.DisplayMember = "name";
columnComboBox.comboBox.ValueMember = "GUID";
columnComboBox.MappingName = "country";
columnComboBox.HeaderText = "Country";
columnComboBox.Width = 200;

// Add the custom TableStyle to the DataGrid.
datagrid.DataSource = table;
tableStyle.MappingName = "TableOne";

I think I have focused on a problem here: if you want a ComboBox in your DataGrid, and you want to populate the ComboBox with items from an array containing instances of your own class.

I hope someone finds it useful - enjoy.

Updated September 2006

A few bugs have been found in my source code. Apparently, someone still downloads and tries to use the source, even though .NET 2.0 has solved the problem with a ComboBox in a DataGrid. The new download contains the original source, plus a small VS project with the updated source code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

René Paw Christensen
Systems / Hardware Administrator
Denmark Denmark

Comments and Discussions

QuestionHow to assign Combobox selcted value to another text cell in datagrid in VB.NET 2005? Pinmemberbhatiamanoj1712-Oct-09 20:56 
GeneralShow a combobox item to be selected by default PinmemberDinesh Girija Sundaram6-Jul-09 22:20 
GeneralRe: Show a combobox item to be selected by default PinmemberDIPAK@EMSYS19-Aug-09 0:18 
Generalcropping images Pinmembernace2k216-Jun-09 21:43 
Questioncombobox should hide after selecting value from combobox and selected value show in datagrid cell Pinmembernik4u21-Jan-09 20:02 
AnswerRe: combobox should hide after selecting value from combobox and selected value show in datagrid cell Pinmembernik4u21-Jan-09 22:17 
GeneralRe: combobox should hide after selecting value from combobox and selected value show in datagrid cell PinmemberRené Paw Christensen22-Jan-09 8:47 
GeneralThe Text is not empty, but the selectIndex is -1 Pinmemberdqddqq1-Nov-07 17:35 
GeneralRe: The Text is not empty, but the selectIndex is -1 Pinmemberdqddqq6-Nov-07 21:43 
GeneralWhy.... Pinmemberpatgrape30-Oct-07 18:21 
GeneralRe: Why.... PinmemberRené Paw Christensen31-Oct-07 6:35 
QuestionInsert Values dynamically? [modified] PinmemberMaYo696-Sep-07 0:11 
QuestionUpdate cell content with the selected index of the ComboBox Pinmemberlaurentma9215-Apr-07 22:59 
AnswerRe: Update cell content with the selected index of the ComboBox Pinmemberlaurentma9216-Apr-07 3:31 
QuestionIt is not working PinmemberDeltaSoft10-Apr-07 19:34 
QuestionHow to get the selected value in the combobox Pinmembermax8425-Mar-07 4:25 
AnswerRe: How to get the selected value in the combobox Pinmemberlaurentma9216-Apr-07 3:24 
GeneralProblem with Focus on the GridComboBox Pinmemberraj30sep7-Nov-06 6:20 
GeneralNew row always has first element in combobox selected PinmemberDensi24-Oct-06 7:14 
GeneralRe: New row always has first element in combobox selected PinmemberDensi25-Oct-06 8:39 
GeneralRe: New row always has first element in combobox selected Pinmemberdqddqq1-Nov-07 20:25 
GeneralCurrentCellChanged Pinmembervaishali1017-Sep-06 21:26 
GeneralCombo Selection Change Event. Pinmemberramya.venkateswaran@gmail.com17-Sep-06 20:50 
GeneralSOURCE: Form1.cs PinmemberRené Paw Christensen12-Sep-06 5:21 
GeneralSOURCE: DataGridComboBoxColumn.cs PinmemberRené Paw Christensen12-Sep-06 5:17 
GeneralDataGridComboBox Working fine Pinmemberjssandeep10-Sep-06 20:46 
GeneralRe: DataGridComboBox Working fine Pinmemberjssandeep10-Sep-06 21:03 
GeneralThis DataGridCombobox has errors Pinmemberjssandeep6-Sep-06 22:58 
GeneralRe: This DataGridCombobox has errors Pinmemberjssandeep10-Sep-06 21:09 
GeneralDataGrid Custom Column Style Pinmemberjssandeep6-Sep-06 1:19 
GeneralRe: DataGrid Custom Column Style PinmemberEric Engler12-Sep-06 2:30 
GeneralData Picker in Datagrid Pinmembertvphuong0514-Jul-06 13:27 
QuestionCan you send me the code in VB .NET Pinmemberfiaolle6-Jul-06 9:01 
QuestionDataGridView in Csharp Pinmemberjcorrales29-Jun-06 6:51 
QuestionWindows DataGridColumns User Controls Pinmemberdbn_0110-Jan-06 17:48 
AnswerRe: Windows DataGridColumns User Controls Pinmemberjssandeep10-Sep-06 21:16 
QuestionHow can i get the ComboBox value PinmemberMunazzahNawaz13-Dec-05 1:24 
Questionhow can I Pinmemberabufool4-Dec-05 11:32 
QuestionNew Row PinmemberKen_McCullough2-Nov-05 10:44 
AnswerRe: New Row PinmemberEric Engler9-May-06 13:17 
GeneralRe: New Row Pinmemberjssandeep10-Sep-06 20:35 
GeneralRe: New Row Pinmemberjssandeep12-Sep-06 0:15 
GeneralWebClient identification PinsussAnonymous5-Jul-05 6:50 
GeneralError after editing cell Pinmembertlvranas23-Jun-05 3:35 
GeneralExample Combobox in datagrid PinsussAnonymous25-Feb-05 10:11 
GeneralWay to go , Rene! (live long and prosper) PinmemberTodd.Harvey31-Jan-05 10:36 
QuestionCan i use this with real database? Pinmemberestrangeiro26-Sep-04 10:09 
GeneralGrid scrolling problem PinmemberCP user25-Aug-04 11:22 
GeneralRe: Grid scrolling problem Pinsussbilberry71 (Pawel Borkowski)7-Mar-05 23:06 
GeneralVS 2003 Designer compatibility PinmemberThosmos23-Jul-04 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 13 Sep 2006
Article Copyright 2004 by René Paw Christensen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid