Click here to Skip to main content
Click here to Skip to main content

A ComboBox in a DataGrid

By , 25 Dec 2002
 

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

About the Author

Jan Wiggers
Web Developer
Netherlands Netherlands
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalcombobox in datagridviewmemberarunasingh123413-May-09 2:02 
GeneralCombobox in Datagrid [modified]memberindian14316-Jan-09 6:54 
GeneralThank youmemberscheidel2113-Apr-08 23:29 
GeneralTKmemberzhouyu53722-Nov-06 22:53 
goodLaugh | :laugh:
QuestionHow to add SelectedIndexChanged [modified]memberpandia2815-Aug-06 4:34 
Question.NET 2.0membersteffen.glaser10-Aug-06 5:45 
QuestionErrorProvider for DataGridmembergn18-May-06 4:38 
GeneralRegarding DataGridmemberdayakar_dn2-May-06 22:55 
GeneralAlternatingBackColormemberKai Michael Wadsack29-Dec-05 8:16 
GeneralThe comboBox column not showing on DGmemberAlphaD11-Oct-05 8:54 
Generalitem selection in combobox controlsussnehap10-Oct-05 20:11 
GeneralThanksmemberPatrickGawley29-Sep-05 3:22 
QuestionCombobox in first column doesn't workmembermcshadow8830-Aug-05 2:58 
GeneralGreatmembercute bird24-Aug-05 8:46 
GeneralError when trying to usemembershof10-Apr-05 17:21 
GeneralRe: Error when trying to usemembergingertez21-Jun-06 4:08 
GeneralSelection "Bug" in ComboBox componentsussJ.J. Hendriksen1-Mar-05 7:31 
QuestionHow can I change A ComboBox in a DataGrid ...memberaoyee25-Feb-05 2:59 
GeneralNew row in DataGridmembermsenkerik2-Dec-04 0:20 
GeneralI can't use itsussg0deus5-Oct-04 6:12 
GeneralThe designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract.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.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.memberpittaman23-Aug-07 21:33 
GeneralProblem when adding elementmemberArch4ngel16-Aug-04 5:31 
GeneralCorrection to this grid Sudhakar Jalli or Jan Wiggers please reply!sussChristian Findlay8-Aug-04 14:14 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply!memberMadhawa10-May-05 22:58 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply!memberkhalasura1-Dec-05 17:40 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply!memberkris41214-Feb-06 18:50 
GeneralRe: Correction to this grid Sudhakar Jalli or Jan Wiggers please reply!memberbill_cs3-Aug-07 4:39 
Generalno DataSource when creating DataGridComboBoxColumnmemberJimmyVanderhaegen15-Jun-04 22:48 
GeneralAlternatingBackColor = Color.LightYellow DataGrid Property Ignored in DataGridComboBoxColumnmemberkembo25-May-04 6:43 
GeneralRe: AlternatingBackColor = Color.LightYellow DataGrid Property Ignored in DataGridComboBoxColumnmemberJimmyVanderhaegen15-Jun-04 23:49 
QuestionDoes anyone have a working example of this that I can look at?memberbob_b20-May-04 8:35 
AnswerRe: Does anyone have a working example of this that I can look at?memberlawsocs20-May-04 22:30 
GeneralGreat Code... Easy!membercoderforrent3-May-04 10:31 
GeneralI got a problemmemberhxxbin23-Apr-04 9:00 
GeneralHelp me to use this class, pleasememberalisura25-Mar-04 0:59 
GeneralOther Code Changemembericarus@jps.net22-Mar-04 4:22 
GeneralDataGrid ComboBox Eventsmembericarus@jps.net22-Mar-04 4:15 
GeneralCode Changemembericarus@jps.net22-Mar-04 4:07 
QuestionHow can I use MSFlexGrid instead of DataGridmemberQuynhGiao3-Feb-04 15:29 
AnswerRe: How can I use MSFlexGrid instead of DataGridmemberPete Burgess4-Mar-04 4:34 
AnswerRe: How can I use MSFlexGrid instead of DataGridmembervanhao4-Mar-04 7:53 
GeneralI need different ComboBox items for one ComboBoxColumnmemberIlia#29-Jan-04 22:29 
GeneralRe: I need different ComboBox items for one ComboBoxColumnmemberEL HACHIMI9-Jun-04 3:26 
GeneralRe: I need different ComboBox items for one ComboBoxColumnmemberschmidro13-Jun-04 13:07 
GeneralChosen text drawn too highmemberMoebius Street13-Jan-04 5:48 
QuestionDataGridComboBox Bug ??sussAnonymous24-Dec-03 7:31 
Generalnot recieving commit call when updatingsussAnonymous8-Dec-03 3:54 
GeneralRe: not recieving commit call when updatingsussDr. Smartee10-Nov-04 5:30 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 26 Dec 2002
Article Copyright 2002 by Jan Wiggers
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid