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

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

Comments and Discussions

 
Generalcombobox in datagridview Pinmemberarunasingh123413-May-09 3:02 
GeneralCombobox in Datagrid [modified] Pinmemberindian14316-Jan-09 7:54 
GeneralThank you Pinmemberscheidel2114-Apr-08 0:29 
GeneralTK Pinmemberzhouyu53722-Nov-06 23:53 
QuestionHow to add SelectedIndexChanged [modified] Pinmemberpandia2815-Aug-06 5:34 
Question.NET 2.0 Pinmembersteffen.glaser10-Aug-06 6:45 
QuestionErrorProvider for DataGrid Pinmembergn18-May-06 5:38 
GeneralRegarding DataGrid Pinmemberdayakar_dn2-May-06 23:55 
GeneralAlternatingBackColor PinmemberKai Michael Wadsack29-Dec-05 9:16 
GeneralThe comboBox column not showing on DG PinmemberAlphaD11-Oct-05 9:54 
Generalitem selection in combobox control Pinsussnehap10-Oct-05 21:11 
GeneralThanks PinmemberPatrickGawley29-Sep-05 4:22 
QuestionCombobox in first column doesn't work Pinmembermcshadow8830-Aug-05 3:58 
GeneralGreat Pinmembercute bird24-Aug-05 9:46 
GeneralError when trying to use Pinmembershof10-Apr-05 18:21 
GeneralRe: Error when trying to use Pinmembergingertez21-Jun-06 5:08 
GeneralSelection "Bug" in ComboBox component PinsussJ.J. Hendriksen1-Mar-05 8:31 
QuestionHow can I change A ComboBox in a DataGrid ... Pinmemberaoyee25-Feb-05 3:59 
GeneralNew row in DataGrid Pinmembermsenkerik2-Dec-04 1:20 
GeneralI can't use it Pinsussg0deus5-Oct-04 7:12 
GeneralThe designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. PinmemberChris Jonas21-Aug-04 9:02 
GeneralRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. PinmemberWooV5-Aug-05 1:14 
AnswerRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. Pinmemberpittaman23-Aug-07 22:33 
GeneralProblem when adding element PinmemberArch4ngel16-Aug-04 6:31 
GeneralCorrection to this grid Sudhakar Jalli or Jan Wiggers please reply! PinsussChristian Findlay8-Aug-04 15:14 

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