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

Multi Column Combo Cell for the .NET 2.0 DataGridView Control

By , 25 Dec 2008
 

Illustration.gif

Introduction

This article will demonstrate an approach to solve the issue of multi-column combobox cells in a DataGridView in VS8.

*New in this version

*After multiple comments that I got about the old implementation, I decided to make this solution nicer. I am following the solution using designed DataSet tables and dropping some workarounds and fixing some bugs I had in my first version!

Background

Several months ago, I invested several days in order to find how to implement the multiline combobox issue. I found several solutions but they didn't fit my needs. I wanted something very simple. And I found it using the owner draw approach - just drawing a multicolumn control by myself. Here are the results.

Using the code

The code implementation and usage is extremely simple.

You do all the steps as you do if you want to embed a regular combobox into your DataGridView, but in place of the DataGridViewComboColumn, you use my class DataGridViewMultiColumnComboColumn. This class is derived from DataGridViewComboColumn. Additionally, you need to set the column CellTemplate with the DataGridViewMultiColumnComboCell class that is derived from DataGridViewMultiColumnComboCell. After creating the DataGridViewMultiColumnComboCell class, you will need to set two data members in order to allow the multiline combobox to display the relevant values.

//create the multicolumncombo column
DataGridViewMultiColumnComboColumn newColumn = 
            new DataGridViewMultiColumnComboColumn();

newColumn.CellTemplate = new DataGridViewMultiColumnComboCell();
//Set the source table settings from the database for combobox values
newColumn.DataSource = ds.LogMessageTypes;
newColumn.DisplayMember = ds.LogMessageTypes.TypeNameColumn.ColumnName;
newColumn.ValueMember = ds.LogMessageTypes.TypeIdColumn.ColumnName;

//this property point on main table of this grid to bind to this column
newColumn.DataPropertyName = ds.LogTable.TypeColumn.ColumnName;
newColumn.HeaderText = ds.LogTable.TypeColumn.ColumnName;

newColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

dataGridView1.Columns.Remove(ds.LogTable.TypeColumn.ColumnName);
dataGridView1.Columns.Insert(position, newColumn);
dataGridView1.Columns[position].Width = 300;

Points of interest

First, I am very happy that after 12 years of programming experience I found a way to contribute something small to this great professional discussion.

I am originally a C++/MFC programmer and I still work mostly using these programming languages. So I am strictly used to things that implement non-standard UIs. I think Microsoft did a great job developing the .NET platform. Anyway.

BTW: I still love developing ActiveX in C++ but there is no client that wants it anymore and I can understand why :).

Disclaimer of warranty

All of the code, information, instructions, and recommendations in this article are offered on a strictly "as is" basis. This material is offered as a free public resource, without any warranty, expressed or implied.

This code is completely free. I will be happy to know if it was helpful for somebody.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

IssaharNoam
Web Developer
Israel Israel
Member
Software Development freelancer.

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   
QuestionSystem.Data.DataRowView when using different datasource for column and Cellsmemberstupsnose_worldwide10 Aug '10 - 5:53 
AnswerRe: System.Data.DataRowView when using different datasource for column and CellsmemberIssaharNoam10 Aug '10 - 10:05 
GeneralRe: System.Data.DataRowView when using different datasource for column and Cellsmemberstupsnose_worldwide10 Aug '10 - 10:10 
AnswerRe: System.Data.DataRowView when using different datasource for column and CellsmemberIssaharNoam10 Aug '10 - 12:11 
QuestionSuggest or SuggestAppendmemberLinoli1 Aug '10 - 3:08 
AnswerRe: Suggest or SuggestAppendmemberIssaharNoam2 Aug '10 - 9:35 
Generalvalue in the cellmembersergio5827 Mar '10 - 4:48 
GeneralRe: value in the cellmemberIssaharNoam28 Mar '10 - 13:37 
GeneralColumns Widthmemberjaneirish22 Mar '10 - 17:34 
GeneralRe: Columns WidthmemberIssaharNoam23 Mar '10 - 11:26 
Generalpopular manuallymembersergio5821 Mar '10 - 5:11 
GeneralRe: popular manuallymemberIssaharNoam21 Mar '10 - 9:41 
GeneralRe: popular manuallymembersergio5822 Mar '10 - 9:05 
GeneralRe: popular manuallymemberIssaharNoam22 Mar '10 - 10:35 
Generaldifferent usememberCICCIOLO6911 Mar '10 - 2:53 
GeneralRe: different usememberIssaharNoam11 Mar '10 - 10:06 
GeneralRe: different usememberCICCIOLO6917 Mar '10 - 5:49 
GeneralRe: different usememberIssaharNoam17 Mar '10 - 9:17 
GeneralRe: different usememberCICCIOLO6918 Mar '10 - 0:19 
GeneralRe: different usememberIssaharNoam18 Mar '10 - 10:54 
GeneralVerry good workmemberbrindus9 Mar '10 - 22:39 
GeneralRe: Verry good workmemberIssaharNoam11 Mar '10 - 10:10 
GeneralHellomemberliviucatrina8 Mar '10 - 3:40 
GeneralRe: HellomemberIssaharNoam8 Mar '10 - 8:18 
GeneralAuto complete feature..memberwithsnaveen25 Oct '09 - 19:56 
GeneralRe: Auto complete feature.. [modified]memberIssaharNoam28 Oct '09 - 11:57 
GeneralSetting the selectedvalue for the dropdown....memberSimon O'Farrell21 Oct '09 - 13:12 
GeneralRe: Setting the selectedvalue for the dropdown....memberIssaharNoam28 Oct '09 - 12:04 
GeneralRe: Setting the selectedvalue for the dropdown....memberSimon O'Farrell28 Oct '09 - 13:28 
GeneralRe: Setting the selectedvalue for the dropdown....memberIssaharNoam29 Oct '09 - 10:44 
Questionhow to get value of Combobox columns in which event of datagridview. and display in netx cellmemberMember 412614914 Oct '09 - 2:56 
AnswerRe: how to get value of Combobox columns in which event of datagridview. and display in netx cellmemberIssaharNoam14 Oct '09 - 11:16 
Questionhow to get value of Combobox columns in which event of datagridview...??? [modified]memberNarendra Reddy Vajrala20 Aug '09 - 3:01 
NewsImportant: My owner draw code contains GDI leaks, Please be aware to dispose all the allocate brushes and pensmemberIssaharNoam9 Apr '09 - 12:56 
GeneralSupporting IList as DataSourcememberKashif Iqbal Khan8 Apr '09 - 4:58 
GeneralDropdown comobox cell in datagridview v2005memberSamik_ Ray2 Apr '09 - 20:51 
GeneralRe: Dropdown comobox cell in datagridview v2005memberIssaharNoam4 Apr '09 - 19:58 
QuestionHow to add headers to combobox columns?membermjbasecamp5 Mar '09 - 9:13 
AnswerRe: How to add headers to combobox columns?memberIssaharNoam5 Mar '09 - 10:12 
GeneralDear Issahar:memberarevans24 Nov '08 - 9:10 
GeneralRe: Dear Issahar:memberIssaharNoam25 Dec '08 - 5:56 
QuestionHow to display in the comboxbox all the itemsmemberdsapo8 Aug '08 - 4:35 
AnswerRe: How to display in the comboxbox all the itemsmemberIssaharNoam25 Dec '08 - 5:58 
QuestionAnyone have some more solid code?memberMember 359872110 Jun '08 - 8:12 
AnswerRe: Anyone have some more solid code?memberIssaharNoam10 Jun '08 - 10:20 
AnswerRe: Anyone have some more solid code?memberIssaharNoam25 Dec '08 - 6:00 
QuestionMultiline comboboxmemberkaandemirtas3 Feb '08 - 7:43 
AnswerRe: Multiline comboboxmemberIssaharNoam25 Dec '08 - 6:00 
QuestionQuestion from the article Multi Column Combo Cell C#, hoping for author to help!memberquakertistar1 Oct '07 - 3:17 
AnswerRe: Question from the article Multi Column Combo Cell C#, hoping for author to help!memberIssaharNoam7 Oct '07 - 5:26 

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.130516.1 | Last Updated 25 Dec 2008
Article Copyright 2007 by IssaharNoam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid