Click here to Skip to main content
13,764,809 members
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 25 Dec 2002

A ComboBox in a DataGrid

, 25 Dec 2002
Rate this:
Please Sign up or sign in to vote.
Descendant from DataGridColumnStyle.


This is a descendant from DataGridColumnStyle and is used to have a ComboBox in a DataGrid column. It is based on an article by Sudhakar Jalli but I found the code not working. I repaired the code and cleaned it up a little.

The DataGridComboBoxColumn class makes it possible to have a ComboBox instead of the default text or checkbox. It took me quite a while to figure it out (even with the help of the article by Sudhakar Jalli). Using it is straightforward.

Using the code

First create a new DataGridTableStyle and make sure the mapping is set to the name of the table in the DataSource where the columns will belong to.

DataGridTableStyle ts=new DataGridTableStyle();

Then, create the DataTable with the lookup values. This could of course come from the database (And it should BTW)

DataTable AccessDataTypes = new DataTable();
AccessDataTypes.Columns.Add(new DataColumn("Number", typeof(int)));
AccessDataTypes.Columns.Add(new DataColumn("Name", typeof(string)));
AccessDataTypes.Rows.Add(new object[] {3, "Numeric"});
AccessDataTypes.Rows.Add(new object[] {130, "Text"});

Create the DataGridComboBoxColumn and add it to the GridColumnStyles. The first argument (Type) is used for the column caption and the mapping. (Could be changed after creating). The second argument (AccessDataTypes) is the DataTable to use for translation. Name and Number are the column names to use from the table and theGrid is the DataGrid where this column will belong to. Be sure to provide the NullText ; if you don't it will throw an exception if you try to add a row to the DataGrid.

DataGridComboBoxColumn c1=new DataGridComboBoxColumn("Type", 
         AccessDataTypes, "Name", "Number", theGrid);

Finally add the newly created TableStyle to the TableStyles array of the DataGrid.


Enjoy the improved DataGrid and be sure to send in your own DataGrid columns!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jan Wiggers
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralI need different ComboBox items for one ComboBoxColumn Pin
Ilia#29-Jan-04 23:29
memberIlia#29-Jan-04 23:29 
GeneralRe: I need different ComboBox items for one ComboBoxColumn Pin
EL HACHIMI9-Jun-04 4:26
memberEL HACHIMI9-Jun-04 4:26 
GeneralRe: I need different ComboBox items for one ComboBoxColumn Pin
schmidro13-Jun-04 14:07
memberschmidro13-Jun-04 14:07 
GeneralChosen text drawn too high Pin
Chris Wuestefeld13-Jan-04 6:48
memberChris Wuestefeld13-Jan-04 6:48 
QuestionDataGridComboBox Bug ?? Pin
Anonymous24-Dec-03 8:31
memberAnonymous24-Dec-03 8:31 
Generalnot recieving commit call when updating Pin
Anonymous8-Dec-03 4:54
memberAnonymous8-Dec-03 4:54 
GeneralRe: not recieving commit call when updating Pin
Dr. Smartee10-Nov-04 6:30
sussDr. Smartee10-Nov-04 6:30 
QuestionDataGridComboBox offsets when inserting a new record? Pin
bcox24-Nov-03 8:02
memberbcox24-Nov-03 8:02 
Cool code, but I'm having a slight problem when adding a new record.

I have a three column DataGrid. The second column has the DataGridComboBox Column. When I add a new record at the bottom of the grid, if I select the ComboBox control first, then try to move to the last field, the ComboBox offsets horizontally to the right covering the column that I would like to edit. If I select the third column first, it works fine.

Can anyone tell me what I'm doing wrong here (see code below)?

private void SetDataGridStyle()

// create the TableStyle
this.dgTSRequirements = new System.Windows.Forms.DataGridTableStyle();
this.dgTSRequirements.MappingName = "BUSINESS_REQUIREMENT";

// add the first normal TextBox ColumnStyle
this.dgCSRequirementID = new DataGridTextBoxColumn();
this.dgCSRequirementID.MappingName = "BUSINESS_REQUIREMENT_ID";
this.dgCSRequirementID.Width = 60;
this.dgCSRequirementID.HeaderText = "Number";


// add the ComboBox ColumnStyle
this.dgCSRequirementType.Width = 128;
this.dgCSRequirementType.HeaderText = "Type";
this.dgCSRequirementType.NullText = "2";


// add the third normal TextBox Column
this.dgCSRequirement = new DataGridTextBoxColumn();
this.dgCSRequirement.MappingName = "BUSINESS_REQUIREMENT";
this.dgCSRequirement.Width = 600;
this.dgCSRequirement.HeaderText = "Requirement";


// add the TableStyle to the DataGrid


I'm stumped on why when adding a new row to the DataGrid the ComboBox column does an offset to the right obscuring the third row so data cannot be added?

QuestionReadOnly property? Pin
Member 100790095-Nov-03 10:06
memberMember 100790095-Nov-03 10:06 
GeneralFormatException raised... Pin
leplayb5-Sep-03 1:21
memberleplayb5-Sep-03 1:21 
GeneralRe: FormatException raised... Pin
JoeGunchy11-Sep-03 13:05
memberJoeGunchy11-Sep-03 13:05 
GeneralRe: FormatException raised... Pin
12-Sep-03 8:53
suss12-Sep-03 8:53 
GeneralRe: FormatException raised... Pin
JoeGunchy16-Sep-03 15:27
memberJoeGunchy16-Sep-03 15:27 
GeneralRe: FormatException raised... Pin
bart_dv7-Jul-04 1:42
memberbart_dv7-Jul-04 1:42 
GeneralEditable ComboBox Pin
JoeGunchy26-Aug-03 11:32
memberJoeGunchy26-Aug-03 11:32 
GeneralRe: Editable ComboBox Pin
JoeGunchy11-Sep-03 13:23
memberJoeGunchy11-Sep-03 13:23 
GeneralRe: Editable ComboBox Pin
robgale2-May-04 19:17
memberrobgale2-May-04 19:17 
GeneralDatagrid Cell Formatting Pin
meatcp6-Aug-03 21:25
membermeatcp6-Aug-03 21:25 
GeneralNullText property Pin
DancnDude9-Jul-03 7:40
memberDancnDude9-Jul-03 7:40 
GeneralRe: NullText property Pin
Chris Wuestefeld23-Nov-03 14:52
memberChris Wuestefeld23-Nov-03 14:52 
Generalkeybord access Pin
wengerp27-May-03 11:04
memberwengerp27-May-03 11:04 
GeneralRe: keybord access Pin
Roger Willcocks16-Sep-03 16:11
memberRoger Willcocks16-Sep-03 16:11 
Generalsetting the alternating background coloe Pin
merojo8-May-03 13:15
membermerojo8-May-03 13:15 
GeneralTo the author!!! Pin
Member 6049431-Mar-03 18:57
memberMember 6049431-Mar-03 18:57 
GeneralRe: To the author!!! Pin
torok9-Apr-03 14:36
membertorok9-Apr-03 14:36 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 26 Dec 2002
Article Copyright 2002 by Jan Wiggers
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid