Contents
In windows forms, a variety of different data structures (that implement the IList interface) can be used to bind data to GUI widgets such as combo boxes or list boxes. Each of these data structures have their pros and cons, the ArrayList saves on features when compared to the DataTable, which results in a slightly faster interface. For the purpose of this article, we will use the ArrayList to populate a ComboBox, and then reference the item IDs when selection is made.
List Item Class
Before we start with the ArrayList, we first need a class with which to store our data. This class should have a few key features:
- Item ID
Will be used to reference an item after it is selected
- Item Name
Will be used to hold the textual display of the item
- ToString override
Required later to reference the selected item of the ComboBox
- IComparable interface
Required for sorting the items in the ArrayList
The following class will implement these items:
private class listitem : IComparable { private Int64 itemid; private string itemname; public listitem(Int64 id, string name) { itemid = id; itemname = name; } public Int64 id { get { return itemid; } } public string name { get { return itemname; } } public override string ToString() { return itemname; } public int CompareTo(object obj) { if (obj is listitem) { listitem other = obj as listitem; return this.itemname.CompareTo(other.itemname); } else { return 0; } } }
Populating the ArrayList
Now that we have our class, we need to add each item in to an ArrayList. If the data is in a database, the easiest and fastest way to do this is by using a DataReader, for each record adding a new listitem to the ArrayList. The following snippet shows this, using a SQLite interface:
ArrayList returnlist = new ArrayList(); string sql = "SELECT VendorID, VendorName FROM Vendor"; SQLiteConnection db = new SQLiteConnection(); db.ConnectionString = "Data Source=TCdev.s3db"; try { db.Open(); SQLiteCommand comm = new SQLiteCommand(sql, db); SQLiteDataReader reader = comm.ExecuteReader(); while (reader.Read()) { returnlist.Add(new listitem( Convert.ToInt64(reader.GetString(reader.GetOrdinal("VendorID"))), reader.GetString(reader.GetOrdinal("VendorName")))); } reader.Close(); }
Binding the ArrayList to the ComboBox
At this point we have our data in an ArrayList, and have the ability to sort it or add new entries if we wish. The following snippet will bind our data to a ComboBox:
returnlist.Add(new listitem(-1, "{Please Select a Vendor}")); returnlist.Sort(); combovendors.Sorted = false; combovendors.DataSource = returnlist; combovendors.DisplayMember = "name"; combovendors.ValueMember = "id";
This will first add another item to the ArrayList asking the user to select a vendor, then sort the ArrayList (using the IComparable interface method that we implemented in the class), and finally bind the data to the ComboBox.
The ComboBox DataSource is the data binding, the DisplayMember is the text that is displayed to the user in the ComboBox, and the ValueMember is the value associated to each DisplayMember. Note that the ValueMember does not have to be numeric; however it is generally more useful as a data retrieval tool if it is.
Referencing Items in the ComboBox
So now our data is in there, how do we react to the user changing the selection? We simply use the SelectedIndexChanged method of the ComboBox (this is the same for a ListBox as well), and reference the SelectedValue of the object:
private void combovendors_SelectedIndexChanged(object sender, EventArgs e) { if (CheckNumeric(combovendors.SelectedValue.ToString())) { MessageBox.Show( combovendors.SelectedValue.ToString() + " - " + combovendors.SelectedItem.ToString()); } }
The relevant information from the ComboBox is now given in a MessageBox. The SelectedValue of the ComboBox will give the ValueMember earlier bound to the listitem id attribute, with the SelectedItem giving the DisplayMember brought through with the ToString method of the listitem class. The SelectedValue (or id field) can then be used to pull different information through from the database, or whatever may be required.
As previously stated, a DataTable has more features than an ArrayList, however for filling a ComboBox or ListBox, we only need a couple of attributes from our data, and therefore an ArrayList is more than adequate (and slightly faster).
References
For more information on using SQLite in C#.NET 4.0, please view this article.