Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Article

A ComboBox in a DataGrid

Rate me:
Please Sign up or sign in to vote.
4.00/5 (59 votes)
25 Dec 20021 min read 439.2K   2.2K   75   79
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.

C#
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)

C#
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.

C#
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.

C#
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


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalcombobox in datagridview Pin
arunasingh123413-May-09 2:02
arunasingh123413-May-09 2:02 
GeneralCombobox in Datagrid [modified] Pin
indian14316-Jan-09 6:54
indian14316-Jan-09 6:54 
GeneralThank you Pin
scheidel2113-Apr-08 23:29
scheidel2113-Apr-08 23:29 
GeneralTK Pin
zhouyu53722-Nov-06 22:53
zhouyu53722-Nov-06 22:53 
QuestionHow to add SelectedIndexChanged [modified] Pin
pandia2815-Aug-06 4:34
pandia2815-Aug-06 4:34 
Question.NET 2.0 Pin
steffen.glaser10-Aug-06 5:45
steffen.glaser10-Aug-06 5:45 
QuestionErrorProvider for DataGrid Pin
gn18-May-06 4:38
gn18-May-06 4:38 
GeneralRegarding DataGrid Pin
dayakar_dn2-May-06 22:55
dayakar_dn2-May-06 22:55 
Hi
In datagrid i have checkbox and combobox, if i selected one or two checkboxes after that i selected one value in the combobox then i will click one button.The datagrid will contains 10 columns, After clikcing this buttion
one of column assume it is 5th column will fill the value which is selected from the combobox value

How can we achieve this if you know kindly mail me to
My mail is dayakar.dn@gmail.com

Regards
Dayakar
GeneralAlternatingBackColor Pin
Kai Michael Wadsack29-Dec-05 8:16
Kai Michael Wadsack29-Dec-05 8:16 
GeneralThe comboBox column not showing on DG Pin
AlphaD11-Oct-05 8:54
AlphaD11-Oct-05 8:54 
Generalitem selection in combobox control Pin
nehap10-Oct-05 20:11
nehap10-Oct-05 20:11 
GeneralThanks Pin
PatrickGawley29-Sep-05 3:22
PatrickGawley29-Sep-05 3:22 
QuestionCombobox in first column doesn't work Pin
Member 185693830-Aug-05 2:58
Member 185693830-Aug-05 2:58 
GeneralGreat Pin
cute bird24-Aug-05 8:46
cute bird24-Aug-05 8:46 
GeneralError when trying to use Pin
Shof10-Apr-05 17:21
Shof10-Apr-05 17:21 
GeneralRe: Error when trying to use Pin
gingertez21-Jun-06 4:08
gingertez21-Jun-06 4:08 
GeneralSelection "Bug" in ComboBox component Pin
Orcus21-Mar-05 7:31
Orcus21-Mar-05 7:31 
QuestionHow can I change A ComboBox in a DataGrid ... Pin
aoyee25-Feb-05 2:59
aoyee25-Feb-05 2:59 
GeneralNew row in DataGrid Pin
msenkerik2-Dec-04 0:20
msenkerik2-Dec-04 0:20 
GeneralI can't use it Pin
g0deus5-Oct-04 6:12
sussg0deus5-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. Pin
Chris Jonas21-Aug-04 8:02
Chris 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
WooV5-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
pittaman23-Aug-07 21:33 
GeneralProblem when adding element Pin
Arch4ngel16-Aug-04 5:31
Arch4ngel16-Aug-04 5:31 
GeneralCorrection to this grid Sudhakar Jalli or Jan Wiggers please reply! Pin
TheChristian8-Aug-04 14:14
TheChristian8-Aug-04 14:14 

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.