Click here to Skip to main content
6,630,901 members and growing! (18,713 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » Combo and List boxes     Intermediate

A ComboBox in a DataGrid

By Jan Wiggers

Descendant from DataGridColumnStyle.
C#, .NET, Win2K, WinXP, Visual Studio, Dev
Posted:25 Dec 2002
Views:293,836
Bookmarked:71 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
58 votes for this article.
Popularity: 6.27 Rating: 3.56 out of 5
6 votes, 10.3%
1
7 votes, 12.1%
2
7 votes, 12.1%
3
20 votes, 34.5%
4
18 votes, 31.0%
5

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


Member

Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 79 (Total in Forum: 79) (Refresh)FirstPrevNext
Generalcombobox in datagridview Pinmemberarunasingh12343:02 13 May '09  
GeneralCombobox in Datagrid [modified] Pinmemberindian1437:54 16 Jan '09  
GeneralThank you Pinmemberscheidel210:29 14 Apr '08  
GeneralTK Pinmemberzhouyu53723:53 22 Nov '06  
QuestionHow to add SelectedIndexChanged [modified] Pinmemberpandia285:34 15 Aug '06  
Question.NET 2.0 Pinmembersteffen.glaser6:45 10 Aug '06  
QuestionErrorProvider for DataGrid Pinmembergn15:38 8 May '06  
GeneralRegarding DataGrid Pinmemberdayakar_dn23:55 2 May '06  
GeneralAlternatingBackColor PinmemberKai Michael Wadsack9:16 29 Dec '05  
GeneralThe comboBox column not showing on DG PinmemberAlphaD9:54 11 Oct '05  
Generalitem selection in combobox control Pinsussnehap21:11 10 Oct '05  
GeneralThanks PinmemberPatrickGawley4:22 29 Sep '05  
QuestionCombobox in first column doesn't work Pinmembermcshadow883:58 30 Aug '05  
GeneralGreat Pinmembercute bird9:46 24 Aug '05  
GeneralError when trying to use Pinmembershof18:21 10 Apr '05  
GeneralRe: Error when trying to use Pinmembergingertez5:08 21 Jun '06  
GeneralSelection "Bug" in ComboBox component PinsussJ.J. Hendriksen8:31 1 Mar '05  
GeneralHow can I change A ComboBox in a DataGrid ... Pinmemberaoyee3:59 25 Feb '05  
GeneralNew row in DataGrid Pinmembermsenkerik1:20 2 Dec '04  
GeneralI can't use it Pinsussg0deus7:12 5 Oct '04  
GeneralThe designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. PinmemberChris Jonas9:02 21 Aug '04  
GeneralRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. PinmemberWooV1:14 5 Aug '05  
AnswerRe: The designer must create an instance of type 'System.Windows.Forms.DataGridColumnStyle' but it cannot because the type is declared as abstract. Pinmemberpittaman22:33 23 Aug '07  
GeneralProblem when adding element PinmemberArch4ngel6:31 16 Aug '04  
GeneralCorrection to this grid Sudhakar Jalli or Jan Wiggers please reply! PinsussChristian Findlay15:14 8 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Dec 2002
Editor: Nishant Sivakumar
Copyright 2002 by Jan Wiggers
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project