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.
DataGridViewMultiColumnComboColumn newColumn =
new DataGridViewMultiColumnComboColumn();
newColumn.CellTemplate = new DataGridViewMultiColumnComboCell();
newColumn.DataSource = ds.LogMessageTypes;
newColumn.DisplayMember = ds.LogMessageTypes.TypeNameColumn.ColumnName;
newColumn.ValueMember = ds.LogMessageTypes.TypeIdColumn.ColumnName;
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.