Click here to Skip to main content
15,885,190 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

 
GeneralDataGrid ComboBox Events Pin
Member 65287322-Mar-04 4:15
Member 65287322-Mar-04 4:15 
GeneralCode Change Pin
Member 65287322-Mar-04 4:07
Member 65287322-Mar-04 4:07 
QuestionHow can I use MSFlexGrid instead of DataGrid Pin
QuynhGiao3-Feb-04 15:29
QuynhGiao3-Feb-04 15:29 
AnswerRe: How can I use MSFlexGrid instead of DataGrid Pin
Pete Burgess4-Mar-04 4:34
Pete Burgess4-Mar-04 4:34 
AnswerRe: How can I use MSFlexGrid instead of DataGrid Pin
vanhao4-Mar-04 7:53
vanhao4-Mar-04 7:53 
GeneralI need different ComboBox items for one ComboBoxColumn Pin
Ilia#29-Jan-04 22:29
Ilia#29-Jan-04 22:29 
GeneralRe: I need different ComboBox items for one ComboBoxColumn Pin
EL HACHIMI9-Jun-04 3:26
EL HACHIMI9-Jun-04 3:26 
GeneralRe: I need different ComboBox items for one ComboBoxColumn Pin
schmidro13-Jun-04 13:07
schmidro13-Jun-04 13:07 
OK, I am not sure if columnComboBox outlined in this artice has the necessary plumbing but I used the component from this website @ http://www.datagridcolumnstyles.net/SuiteI.asp and with the ListFilter event I was able to use different values in each row ... here is also a demo example to review ... http://www.datagridcolumnstyles.net/examples.asp ... titled 'Filter ComboBox Filters'.

On caviar ... the fields need to be of the same datatype ... not sure how critical this is to you.

Good luck and hope this helps.

Robert
GeneralChosen text drawn too high Pin
Chris Wuestefeld13-Jan-04 5:48
Chris Wuestefeld13-Jan-04 5:48 
QuestionDataGridComboBox Bug ?? Pin
Anonymous24-Dec-03 7:31
Anonymous24-Dec-03 7:31 
Generalnot recieving commit call when updating Pin
Anonymous8-Dec-03 3:54
Anonymous8-Dec-03 3:54 
GeneralRe: not recieving commit call when updating Pin
Dr. Smartee10-Nov-04 5:30
sussDr. Smartee10-Nov-04 5:30 
QuestionDataGridComboBox offsets when inserting a new record? Pin
bcox24-Nov-03 7:02
bcox24-Nov-03 7:02 
QuestionReadOnly property? Pin
Member 100790095-Nov-03 9:06
Member 100790095-Nov-03 9:06 
GeneralFormatException raised... Pin
leplayb5-Sep-03 0:21
leplayb5-Sep-03 0:21 
GeneralRe: FormatException raised... Pin
JoeGunchy11-Sep-03 12:05
JoeGunchy11-Sep-03 12:05 
GeneralRe: FormatException raised... Pin
12-Sep-03 7:53
suss12-Sep-03 7:53 
GeneralRe: FormatException raised... Pin
JoeGunchy16-Sep-03 14:27
JoeGunchy16-Sep-03 14:27 
GeneralRe: FormatException raised... Pin
bart_dv7-Jul-04 0:42
bart_dv7-Jul-04 0:42 
GeneralEditable ComboBox Pin
JoeGunchy26-Aug-03 10:32
JoeGunchy26-Aug-03 10:32 
GeneralRe: Editable ComboBox Pin
JoeGunchy11-Sep-03 12:23
JoeGunchy11-Sep-03 12:23 
GeneralRe: Editable ComboBox Pin
robgale2-May-04 18:17
robgale2-May-04 18:17 
GeneralDatagrid Cell Formatting Pin
meatcp6-Aug-03 20:25
meatcp6-Aug-03 20:25 
GeneralNullText property Pin
DancnDude9-Jul-03 6:40
DancnDude9-Jul-03 6:40 
GeneralRe: NullText property Pin
Chris Wuestefeld23-Nov-03 13:52
Chris Wuestefeld23-Nov-03 13:52 

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.