Click here to Skip to main content
15,880,427 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 193.5K   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

 
QuestionHow to mention specific columns from the datatable in the combobox? Pin
vikki.iiit2-Jun-20 3:54
vikki.iiit2-Jun-20 3:54 
QuestionAutoComplete Pin
murat bayram13-Mar-19 4:21
murat bayram13-Mar-19 4:21 
Questionhow to create Combox Multi column In Datagridview column windows forms Pin
Member 1195074614-Sep-15 4:20
Member 1195074614-Sep-15 4:20 
QuestionColumns Header Text Pin
Biplob Singha Shee9-Feb-14 22:01
Biplob Singha Shee9-Feb-14 22:01 
Suggestionadd property to the code Pin
Ahmed Abd EL_Hameed25-Jan-14 0:13
Ahmed Abd EL_Hameed25-Jan-14 0:13 
SuggestionRe: add property to the code Pin
IssaharNoam27-Jan-14 9:11
IssaharNoam27-Jan-14 9:11 
GeneralRe: add property to the code Pin
Ahmed Abd EL_Hameed10-Feb-14 10:01
Ahmed Abd EL_Hameed10-Feb-14 10:01 
GeneralRe: add property to the code Pin
IssaharNoam11-Feb-14 1:19
IssaharNoam11-Feb-14 1:19 
QuestionValues of other members Pin
YasminMCA7-Jan-14 4:00
YasminMCA7-Jan-14 4:00 
AnswerRe: Values of other members Pin
IssaharNoam15-Jan-14 9:52
IssaharNoam15-Jan-14 9:52 
QuestionSystem.Data.DataRowView when using different datasource for column and Cells Pin
stupsnose_worldwide10-Aug-10 5:53
stupsnose_worldwide10-Aug-10 5:53 
AnswerRe: System.Data.DataRowView when using different datasource for column and Cells Pin
IssaharNoam10-Aug-10 10:05
IssaharNoam10-Aug-10 10:05 
GeneralRe: System.Data.DataRowView when using different datasource for column and Cells Pin
stupsnose_worldwide10-Aug-10 10:10
stupsnose_worldwide10-Aug-10 10:10 
AnswerRe: System.Data.DataRowView when using different datasource for column and Cells Pin
IssaharNoam10-Aug-10 12:11
IssaharNoam10-Aug-10 12:11 
QuestionSuggest or SuggestAppend Pin
Linoli1-Aug-10 3:08
Linoli1-Aug-10 3:08 
AnswerRe: Suggest or SuggestAppend Pin
IssaharNoam2-Aug-10 9:35
IssaharNoam2-Aug-10 9:35 
Generalvalue in the cell Pin
sergio5827-Mar-10 4:48
sergio5827-Mar-10 4:48 
GeneralRe: value in the cell Pin
IssaharNoam28-Mar-10 13:37
IssaharNoam28-Mar-10 13:37 
GeneralColumns Width Pin
janeirish22-Mar-10 17:34
janeirish22-Mar-10 17:34 
Sir,

I like the control, i have a question how can i modify the columns width per column. example column1=100 and column2=50

Thank you very much.
GeneralRe: Columns Width Pin
IssaharNoam23-Mar-10 11:26
IssaharNoam23-Mar-10 11:26 
Generalpopular manually Pin
sergio5821-Mar-10 5:11
sergio5821-Mar-10 5:11 
GeneralRe: popular manually Pin
IssaharNoam21-Mar-10 9:41
IssaharNoam21-Mar-10 9:41 
GeneralRe: popular manually Pin
sergio5822-Mar-10 9:05
sergio5822-Mar-10 9:05 
GeneralRe: popular manually Pin
IssaharNoam22-Mar-10 10:35
IssaharNoam22-Mar-10 10:35 
Generaldifferent use Pin
CICCIOLO6911-Mar-10 2:53
CICCIOLO6911-Mar-10 2:53 

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.