Click here to Skip to main content
15,867,957 members
Articles / Programming Languages / C#
Tip/Trick

How to Use a Custom Grid with ComboBox Columns

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Feb 2014CPOL 9.9K   4  
DataGridView and ComboBox

Introduction

I'm a lazy person. I don't want to write a lot of code, but sometimes I have to, especially when I try to use free software with Windows.

Background

This tip has been created after a long time while I'm trying to combine MySQL database and Microsoft Excel in one C# application for non-IT users.

Using the Code

This code has been created and tested in a custom DataGridView without underlining DataSource in Microsoft Visual C# 2010 (Express).

  1. Declare class for combo box item in your project. This class must have at least two properties (not a data member):
    C#
    class CBItemClass
    {
        long id;
        string name;
        public long ID
        {
            get { return id;}
            set {id = value;}
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    
  2. Declare combo box source collection in your form:
    C#
    List<CBItemClass> xlCols2;
    
  3. Fill this collection with items you need (I assume that you create an instance for your data source before you try to fill it):
    C#
    CBItemClass o = new CBItemClass();
    xlCols2.Add(o);
    
  4. Prepare your DataGridView's column to use items you load:
    C#
    col.ValueMember = "ID"; // this property will be used as a key */
    col.DisplayMember = "Name";// this property will display to user even in drop down list
    col.DataSource = xlCols2; // connect collection with the column
    
  5. And, when you need - you can set a value that you need in combo with similar code:
    C#
    CBItemClass o = new CBItemClass(); // create an item
    o.ID = ...; // set primary key
    o.Name = ...;// set display name
    dataGrid.Rows[1].Cells[1].Value = o; // set cell's value
    
  6. After all, you'll get a grid with your values.

Points of Interest

I wrote this tip after I spent half a day to search a solution for my issue.

History

  • 2014-02-10 17:50:00 : Initial post

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) NetCracker
Russian Federation Russian Federation
I came to the industry at the end of that times when the computer program executes as it was written. I saw a quite big machines, occupied more than 100 square meters for its central processor, but I started my professional activity as a programmer on IBM PC clones. There were different CPU architectures (68k, PowerPC, 386/486, SPARC...) when I began, but Intel defeated them all with Pentium CPU (marketing) family.
I saw the knowledge and technology fragmentation. I saw many technologies started, developed and retired. However, I have not seen many things yet.
I have some experience, but my experience is not perfectly comprehensive. I still have many things to learn and I still cannot make a poker face when I find out some aspects about how the things were designed and works. My experience does not make me an absolute expert in some areas, because these areas are changing. I know some things, but I also understand that some things I know could be useless for nowadays.

Comments and Discussions

 
-- There are no messages in this forum --