Click here to Skip to main content
Licence CPOL
First Posted 9 Apr 2007
Views 79,177
Downloads 2,374
Bookmarked 59 times

Multi Column Combo Cell for the .NET 2.0 DataGridView Control

By | 25 Dec 2008 | Article
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.

//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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSystem.Data.DataRowView when using different datasource for column and Cells Pinmemberstupsnose_worldwide5:53 10 Aug '10  
AnswerRe: System.Data.DataRowView when using different datasource for column and Cells PinmemberIssaharNoam10:05 10 Aug '10  
GeneralRe: System.Data.DataRowView when using different datasource for column and Cells Pinmemberstupsnose_worldwide10:10 10 Aug '10  
AnswerRe: System.Data.DataRowView when using different datasource for column and Cells PinmemberIssaharNoam12:11 10 Aug '10  
QuestionSuggest or SuggestAppend PinmemberLinoli3:08 1 Aug '10  
AnswerRe: Suggest or SuggestAppend PinmemberIssaharNoam9:35 2 Aug '10  
Generalvalue in the cell Pinmembersergio584:48 27 Mar '10  
GeneralRe: value in the cell PinmemberIssaharNoam13:37 28 Mar '10  
GeneralColumns Width Pinmemberjaneirish17:34 22 Mar '10  
GeneralRe: Columns Width PinmemberIssaharNoam11:26 23 Mar '10  
Generalpopular manually Pinmembersergio585:11 21 Mar '10  
GeneralRe: popular manually PinmemberIssaharNoam9:41 21 Mar '10  
GeneralRe: popular manually Pinmembersergio589:05 22 Mar '10  
GeneralRe: popular manually PinmemberIssaharNoam10:35 22 Mar '10  
Generaldifferent use PinmemberCICCIOLO692:53 11 Mar '10  
GeneralRe: different use PinmemberIssaharNoam10:06 11 Mar '10  
GeneralRe: different use PinmemberCICCIOLO695:49 17 Mar '10  
GeneralRe: different use PinmemberIssaharNoam9:17 17 Mar '10  
GeneralRe: different use PinmemberCICCIOLO690:19 18 Mar '10  
GeneralRe: different use PinmemberIssaharNoam10:54 18 Mar '10  
GeneralVerry good work Pinmemberbrindus22:39 9 Mar '10  
GeneralRe: Verry good work PinmemberIssaharNoam10:10 11 Mar '10  
GeneralHello Pinmemberliviucatrina3:40 8 Mar '10  
GeneralRe: Hello PinmemberIssaharNoam8:18 8 Mar '10  
GeneralAuto complete feature.. Pinmemberwithsnaveen19:56 25 Oct '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 Dec 2008
Article Copyright 2007 by IssaharNoam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid