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

Multi Column Combo Cell for the .NET 2.0 DataGridView Control

Rate me:
Please Sign up or sign in to vote.
3.19/5 (14 votes)
25 Dec 2008CPOL2 min read 194.4K   9.7K   70   66
This article will demonstrate an approach to solve the issue of a multi-column cell for DataGridView.

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.

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


Written By
Software Developer (Senior)
Israel Israel
Software Development freelancer.

Comments and Discussions

 
GeneralRe: Auto complete feature.. [modified] Pin
IssaharNoam28-Oct-09 11:57
IssaharNoam28-Oct-09 11:57 
GeneralSetting the selectedvalue for the dropdown.... Pin
Simon O'Farrell21-Oct-09 13:12
Simon O'Farrell21-Oct-09 13:12 
GeneralRe: Setting the selectedvalue for the dropdown.... Pin
IssaharNoam28-Oct-09 12:04
IssaharNoam28-Oct-09 12:04 
GeneralRe: Setting the selectedvalue for the dropdown.... Pin
Simon O'Farrell28-Oct-09 13:28
Simon O'Farrell28-Oct-09 13:28 
GeneralRe: Setting the selectedvalue for the dropdown.... Pin
IssaharNoam29-Oct-09 10:44
IssaharNoam29-Oct-09 10:44 
Questionhow to get value of Combobox columns in which event of datagridview. and display in netx cell Pin
PankajRai.Net14-Oct-09 2:56
PankajRai.Net14-Oct-09 2:56 
AnswerRe: how to get value of Combobox columns in which event of datagridview. and display in netx cell Pin
IssaharNoam14-Oct-09 11:16
IssaharNoam14-Oct-09 11:16 
Questionhow to get value of Combobox columns in which event of datagridview...??? [modified] Pin
Narendra Reddy Vajrala20-Aug-09 3:01
Narendra Reddy Vajrala20-Aug-09 3:01 
Hello, Isshar!
this article was really helped me alot in coding n time also
very thanks

a small requirement:
am displaying two columns in multicolumncombobox as
newColumn.DataPropertyName = Sds.Tables[0].Columns[0].ColumnName;<br />
            newColumn.DisplayMember = Sds.Tables[0].Columns[0].ColumnName;<br />
            newColumn.ValueMember = Sds.Tables[0].Columns[1].ColumnName;

in some other datagridview events i want to retrieve these two values

based on the section of that combobox value i have to fill the remaining cell values to that perticular row.

i wrote code in dataGridView1_CellClick event
{
if (e.ColumnIndex == 1)
{

int rowNo = e.RowIndex;
string prId = dataGridView1.Rows[rowNo].Cells[1].Value.ToString();

based on prId am retrieving values and filling remaining cells to that row.
}
}


but the problem is
1) it is not filling the remaining cells at the time of selecting that perticular comboboxcolumn
2)If i leave that cell and comeagain to that cell then remaining cells were filling

i tried in cell Leave event and cellValuechanged event also but no use
am sure that i am calling this in some wrong event which is not suitable to this requirement
can anybody shed some light on this
on which Datagridview event i have to call this to effect that row based on the selection of cell(comboboxColumn cell)

how can i do this???
please reply me

Thankyou very much once again
--Naren

modified on Tuesday, December 1, 2009 6:39 AM

NewsImportant: My owner draw code contains GDI leaks, Please be aware to dispose all the allocate brushes and pens Pin
IssaharNoam9-Apr-09 12:56
IssaharNoam9-Apr-09 12:56 
GeneralSupporting IList as DataSource Pin
Kashif Iqbal Khan8-Apr-09 4:58
Kashif Iqbal Khan8-Apr-09 4:58 
GeneralDropdown comobox cell in datagridview v2005 Pin
Samik_ Ray2-Apr-09 20:51
Samik_ Ray2-Apr-09 20:51 
GeneralRe: Dropdown comobox cell in datagridview v2005 Pin
IssaharNoam4-Apr-09 19:58
IssaharNoam4-Apr-09 19:58 
QuestionHow to add headers to combobox columns? Pin
mjbasecamp5-Mar-09 9:13
mjbasecamp5-Mar-09 9:13 
AnswerRe: How to add headers to combobox columns? Pin
IssaharNoam5-Mar-09 10:12
IssaharNoam5-Mar-09 10:12 
GeneralDear Issahar: Pin
arevans24-Nov-08 9:10
arevans24-Nov-08 9:10 
GeneralRe: Dear Issahar: Pin
IssaharNoam25-Dec-08 5:56
IssaharNoam25-Dec-08 5:56 
QuestionHow to display in the comboxbox all the items Pin
dsapo8-Aug-08 4:35
dsapo8-Aug-08 4:35 
AnswerRe: How to display in the comboxbox all the items Pin
IssaharNoam25-Dec-08 5:58
IssaharNoam25-Dec-08 5:58 
QuestionAnyone have some more solid code? Pin
Member 359872110-Jun-08 8:12
Member 359872110-Jun-08 8:12 
AnswerRe: Anyone have some more solid code? Pin
IssaharNoam10-Jun-08 10:20
IssaharNoam10-Jun-08 10:20 
AnswerRe: Anyone have some more solid code? Pin
IssaharNoam25-Dec-08 6:00
IssaharNoam25-Dec-08 6:00 
QuestionMultiline combobox Pin
kaandemirtas3-Feb-08 7:43
kaandemirtas3-Feb-08 7:43 
AnswerRe: Multiline combobox Pin
IssaharNoam25-Dec-08 6:00
IssaharNoam25-Dec-08 6:00 
QuestionQuestion from the article Multi Column Combo Cell C#, hoping for author to help! Pin
quakertistar1-Oct-07 3:17
quakertistar1-Oct-07 3:17 
AnswerRe: Question from the article Multi Column Combo Cell C#, hoping for author to help! Pin
IssaharNoam7-Oct-07 5:26
IssaharNoam7-Oct-07 5:26 

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.