Click here to Skip to main content
13,198,850 members (44,148 online)
Click here to Skip to main content
Add your own
alternative version

Stats

387.9K views
1.8K downloads
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.

Introduction

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();
ts.MappingName="Columns";

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);
c1.NullText="3";
ts.GridColumnStyles.Add(c1);

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

theGrid.TableStyles.Add(ts);

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

License

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

Share

About the Author

Jan Wiggers
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThe designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. Pin
Chris Jonas21-Aug-04 8:02
memberChris Jonas21-Aug-04 8:02 
GeneralRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. Pin
WooV5-Aug-05 0:14
memberWooV5-Aug-05 0:14 
AnswerRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. Pin
pittaman23-Aug-07 21:33
memberpittaman23-Aug-07 21:33 
GeneralProblem when adding element Pin
Arch4ngel16-Aug-04 5:31
memberArch4ngel16-Aug-04 5:31 
GeneralCorrection to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
Christian Findlay8-Aug-04 14:14
sussChristian Findlay8-Aug-04 14:14 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
Madhawa10-May-05 22:58
memberMadhawa10-May-05 22:58 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
khalasura1-Dec-05 17:40
memberkhalasura1-Dec-05 17:40 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
kris41214-Feb-06 18:50
memberkris41214-Feb-06 18:50 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
bill_cs3-Aug-07 4:39
memberbill_cs3-Aug-07 4:39 
Generalno DataSource when creating DataGridComboBoxColumn Pin
JimmyVanderhaegen15-Jun-04 22:48
memberJimmyVanderhaegen15-Jun-04 22:48 
GeneralAlternatingBackColor = Color.LightYellow DataGrid Property Ignored in DataGridComboBoxColumn Pin
kembo25-May-04 6:43
memberkembo25-May-04 6:43 
GeneralRe: AlternatingBackColor = Color.LightYellow DataGrid Property Ignored in DataGridComboBoxColumn Pin
JimmyVanderhaegen15-Jun-04 23:49
memberJimmyVanderhaegen15-Jun-04 23:49 
QuestionDoes anyone have a working example of this that I can look at? Pin
bob_b20-May-04 8:35
memberbob_b20-May-04 8:35 
AnswerRe: Does anyone have a working example of this that I can look at? Pin
lawsocs20-May-04 22:30
memberlawsocs20-May-04 22:30 
GeneralGreat Code... Easy! Pin
coderforrent3-May-04 10:31
membercoderforrent3-May-04 10:31 
GeneralI got a problem Pin
hxxbin23-Apr-04 9:00
memberhxxbin23-Apr-04 9:00 
GeneralHelp me to use this class, please Pin
alisura25-Mar-04 0:59
memberalisura25-Mar-04 0:59 
GeneralOther Code Change Pin
icarus@jps.net22-Mar-04 4:22
membericarus@jps.net22-Mar-04 4:22 
GeneralDataGrid ComboBox Events Pin
icarus@jps.net22-Mar-04 4:15
membericarus@jps.net22-Mar-04 4:15 
GeneralCode Change Pin
icarus@jps.net22-Mar-04 4:07
membericarus@jps.net22-Mar-04 4:07 
QuestionHow can I use MSFlexGrid instead of DataGrid Pin
QuynhGiao3-Feb-04 15:29
memberQuynhGiao3-Feb-04 15:29 
AnswerRe: How can I use MSFlexGrid instead of DataGrid Pin
Pete Burgess4-Mar-04 4:34
memberPete Burgess4-Mar-04 4:34 
AnswerRe: How can I use MSFlexGrid instead of DataGrid Pin
vanhao4-Mar-04 7:53
membervanhao4-Mar-04 7:53 
GeneralI need different ComboBox items for one ComboBoxColumn Pin
Ilia#29-Jan-04 22:29
memberIlia#29-Jan-04 22:29 
GeneralRe: I need different ComboBox items for one ComboBoxColumn Pin
EL HACHIMI9-Jun-04 3:26
memberEL HACHIMI9-Jun-04 3:26 

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 | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 26 Dec 2002
Article Copyright 2002 by Jan Wiggers
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid