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).
- Declare class for combo box item in your project. This class must have at least two properties (not a data member):
class CBItemClass
{
long id;
string name;
public long ID
{
get { return id;}
set {id = value;}
}
public string Name
{
get { return name; }
set { name = value; }
}
}
- Declare combo box source collection in your form:
List<CBItemClass> xlCols2;
- Fill this collection with items you need (I assume that you create an instance for your data source before you try to fill it):
CBItemClass o = new CBItemClass();
xlCols2.Add(o);
- Prepare your
DataGridView
's column to use items you load:
col.ValueMember = "ID";
col.DisplayMember = "Name";
col.DataSource = xlCols2;
- And, when you need - you can set a value that you need in combo with similar code:
CBItemClass o = new CBItemClass();
o.ID = ...;
o.Name = ...;
dataGrid.Rows[1].Cells[1].Value = o;
- 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