Multi Column Combo Cell for a .NET 2.0 DataGridView Control

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

The code implementation and usage is extremely simple.

*New in this version

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

You do all the steps as you do if you want to embed the regular combobox into your DataGridView, but in place of the DataGridViewComboColumn, you use my DataGridViewMultiColumnComboColumn class. 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 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;

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

I am originally a C++/MFC programmer and I still work mostly in these programming languages. So, I was strictly used to thinking that implementing a non-standard UI is a piece of work. So, I think Microsoft did a great job when it developed the .NET platform.

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 it it was helpful for somebody.



About the Author

Issahar Noam Gourfinkel

Sofware Development Freelancer

Downloads

Comments

  • IT Administrator

    Posted by vinod on 11/19/2014 08:24pm

    Thanks a lot for placing the code. please help how can I get gridview combobox selected event to get all the three columns value inside the combobox Thanks.

    Reply
  • Thanks a lot

    Posted by Arun on 06/22/2012 04:05am

    Very useful article. Thanks

    Reply
  • thank you

    Posted by Tran Cao Thang on 04/13/2012 03:10am

    I happy from the article: "Multi Column Combo Cell for a .NET 2.0 DataGridView Control" I am Vietnamese so My english is not good to write more than I Happy

    Reply
  • mysql

    Posted by narcisofaria on 06/01/2011 07:13am

    is it possible for it to work with a mysql database Greetings Narcissus would

    Reply
  • ds

    Posted by narcisofaria on 06/01/2011 05:28am

    good day
    
    the class is actually very well done, so I do not understand the code above that is the subject: ds?
    If I can clarify ffectiveness very grateful.
    
    Greetings
    Narciso Faria

    Reply
  • Really helped me but.....

    Posted by narendra_vajrala@yahoo.co.in on 08/20/2009 08:55am

    Hello, Isshar!
    this article was really helped me alot in coding n time also
    very thanks
    
    a small requirement:
    in my c#.net 3.5 windows application, i have a datagridview
    to that datagridview i added a datagridviewcomboboxcolumn, values comming everything fine till here.
    
    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)
    
    please help me out
    
    Thankyou very much once again
    --Naren
    vnreddy@vercom.com

    • Response

      Posted by IssaharNoam on 08/23/2009 05:29pm

      Hi, Naren! If I am not wrong, you should listen for EditingControlShowing DataGridEvent and cast the event Control property to combobox and register your own events to listen it's events. Don't forget to unregister these events on CellEndEdit event. Issahar

      Reply
    Reply
  • checkbox column

    Posted by Buster95 on 05/16/2007 09:22pm

    Very Nice. Add a checkbox column?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds