
Introduction
I am a beginner in C# for 2 months. And recently, I had a hard time to find
out how to set values into ComboBox items. I was looking
for the answer from books, sample code in Internet and MSDN. We can make it
display Primary ID instead of some meaningful names, and that was not what I
wanted. I do not want to see meaningless ID anymore in my life! The hint to find
it out is in the MSDN. You can find a code in the ValueMember section. But the code is 80%. Something is missing...
I tried to find it out, but I could not get it for a week. And finally when I
tried to create a class accidentally, a miracle happened! Anyway I hope I can
help people not to suffer like I did.
Create a sample software
We are going to create a sample software (see the picture) which has a ComboBox for author's name list and when you select an author
name, it is displaying book titles into a ListBox. Key
point is, of course, that I did not display Author ID but Author name. In
addition, I use ADO.NET to store the data, so we can study it, too :-)
Prepare database
I use MS Access to store the data. And tblBooks has
foreign key (AuthorID) to make a relationship with AuthorID in the tblAuthors.
| books.mdb (location =>
/bin/debug/books.mdb) |
| tblAuthors |
| AuthorID |
AuthorName |
| 1 |
Tomohiro |
| 2 |
Henry |
| 3 |
Ben |
| 4 |
Jungwon | |
| tblBooks |
| BookID |
AuthorID |
Title |
| 1 |
1 |
Hello World |
| 2 |
1 |
Japanese Culture |
| 3 |
1 |
Fujisan |
| 4 |
2 |
Spider |
| 5 |
2 |
CDR |
| 6 |
2 |
Beginning VB6 |
| 7 |
3 |
Smart Business |
| 8 |
3 |
MBA holder |
| 9 |
3 |
Web Business |
| 10 |
4 |
English 666 |
| 11 |
4 |
Korean 102 |
| 12 |
4 |
How to be beautiful like me |
| 13 |
4 |
Precalculous for even stupids |
| 14 |
4 |
Super duper cooking | |
Create interface
Now it is time to start! You need to make sure you have a ComboBox (cboAutors) and ListBox (lstBooks). And certainly, you can
change interface to whatever you like, but again, remember, you have to have the
two controls.
ComboBox - cboAutors
ListBox - lstBooks
Create module level values
Well, we need to create two module level values first. And then you give a
type and a path for database to the connection value. And also you need to make
sure to add Using System.Data.OleDb on the top.
Values entry:
private bool AuthorsHaveBeenAdded - Make sure your
cboAuthors ComboBox built.
private OleDbConnection conn - Connection to the
Books.mdb
private OleDbConnection conn;
private bool AuthorsHaveBeenAdded=false;
public Form1()
{
InitializeComponent();
this.conn = new OleDbConnection("
PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Books.mdb;");
Bulid cboAutors ComboBox
Now we will set an AuthorID for valueMember, and an Author Name for DisplayMember. The order is to read data from database, to pass
the data into ArrayList and to pass to cboAuthors ComboBox. And we will create an
AddValue function later.
Values entry:
OleDbCommand cmd - Create SQL command
OleDbDataReader rsAutors - Read tblAuthors table
ArrayList Authors - Store Authors table data
private void buildcboAuthors()
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM tblAuthors",conn);
this.conn .Open ();
OleDbDataReader rsAutors = cmd.ExecuteReader();
ArrayList Authors = new ArrayList();
while(rsAutors.Read())
{
Authors.Add (new AddValue
(rsAutors.GetString(1),rsAutors.GetInt32 (0)));
}
rsAutors.Close();
this.conn.Close();
this.cboAuthors.DataSource = Authors;
this.cboAuthors .DisplayMember ="Display";
this.cboAuthors.ValueMember = "Value";
AuthorsHaveBeenAdded=true;
}
Then call buildcboAuthors().
InitializeComponent();
this.conn = new OleDbConnection(
"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Books.mdb;");
buildcboAuthors();
}
Create AddValue Class
This is the one I spent a week. This is simple, just getting values and
creating properties.
public class AddValue
{
private string m_Display;
private long m_Value;
public AddValue(string Display, long Value)
{
m_Display = Display;
m_Value = Value;
}
public string Display
{
get{return m_Display;}
}
public long Value
{
get{return m_Value;}
}
}
Build lstBooks ListBox
Now we will display book titles. We can use WHERE to
display what we want. And call buidlstBooks in the cboAuthors_SelectedIndexChanged event.
Values entry:
OleDbCommand cmd- Create SQL command
OleDbDataReader rsBooks - Read tblBooks table
private void buildlstBooks()
{
this.lstBooks.Items.Clear();
OleDbCommand cmd = new OleDbCommand(
"SELECT * FROM tblBooks WHERE AuthorID =" +
this.cboAuthors.SelectedValue,conn);
this.conn .Open ();
OleDbDataReader rsBooks = cmd.ExecuteReader();
while(rsBooks.Read ())
{
this.lstBooks .Items.Add (rsBooks.GetString (2));
}
rsBooks.Close ();
this.conn.Close ();
}
private void cboAuthors_SelectedIndexChanged
(object sender, System.EventArgs e)
{
if(this.AuthorsHaveBeenAdded)
buildlstBooks();
}
Conclution
I guess that's it. Happy programming! :0)