Binding a Windows Forms ComboBox in C#

Windows Forms

Most often when reading the selected item of a bound combobox you will need more information than just the selected text or the selected index of the combo. For example, if you have a combobox bound to a user table in your database, you will most probably want to have the full user name displayed in the combobox, but when a user is selected you will want to work with the user code or user id. In this case the combo’s selected index is of no use and neither is the display text. You need a way to be able to retrieve the user code when a user is selected.

Fortunately, this is quite easy to accomplish. All we need to do is bind our combobox to a list of KeyValuePair objects. In this article I am going to show you how to do exactly that.

To start off create a form which looks similar to the below one:

Binding a ComboBox

Now in the Form_Load event handler we must add the following code:

private void MainForm_Load(object sender, EventArgs e)
{
    // Create a List to store our KeyValuePairs
    List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();

    // Add data to the List
    data.Add(new KeyValuePair<string, string>("p1", "Joe"));
    data.Add(new KeyValuePair<string, string>("p2", "David"));
    data.Add(new KeyValuePair<string, string>("p3", "Keith"));
    data.Add(new KeyValuePair<string, string>("p4", "Andrew"));
    data.Add(new KeyValuePair<string, string>("p5", "Maria"));
    
    // Clear the combobox
    cboData.DataSource = null;
    cboData.Items.Clear();

    // Bind the combobox
    cboData.DataSource = new BindingSource(data, null);
    cboData.DisplayMember = "Value";
    cboData.ValueMember = "Key";
}

[continue reading…]

15 comments

C# Escape Sequence Listing

String Operations

What is an escape sequence? Well, put simply, an escape sequence is a series of special characters which are interpreted by the compiler as a command. In other words, they suspend the normal processing to perform some special function.

In C#, escape sequences are represented by a ‘\’ (backslash) followed by a letter or a combination of digits.

The following table represents the most common escape sequences used in C#.

Escape Sequence What it Represents
\’ Single Quotation Mark (character 39)
\” Double Quotation Mark (character 34)
\? Literal Question Mark
\\ Backslash (character 92)
\a Alert/Bell (character 7)
\b Backspace (character 8 )
\f Formfeed (character 12)
\n New Line (character 10)
\r Carriage Return (character 13)
\t Horizontal Tab (character 9)
\v Vertical Tab (character 11)
\ooo ASCII character in octal notation
\xhh ASCII character in hexadecimal notation

[continue reading…]

7 comments

Retrieving Data From a MySQL Database

Databases

In this article I am going to show you how to programmatically retrieve data from a MySQL database using the MySqlDataAdapter and the MySqlDataReader classes. Both these classes are available once you install the MySQL Connector for .NET which can be downloaded from here: MySQL Connectors.

For this example we need a database with some test data. MySQL has an sql script which creates a set of tables with world country information. I will be using these tables for this example so I suggest you download this script and run it on your MySQL database. Once done, you will have added the following three tables to your database.

Schema Diagram

[continue reading…]

14 comments

Connecting to a MySQL Database Programmatically

Databases

Microsoft Visual Studio lets you create a database connection using its IDE, and it’s quite powerful as well, but personally I prefer to create my database connections programmatically. When creating my connection’s code manually I find it easier to follow the code, plus it can be easier to debug as well. I am by no means saying that this is the best way to connect to a database – it’s just my personal preference.

In this article I will show you how to connect to a MySQL database programmatically through C#.

Why MySQL?

I personally like working with MySQL because it’s freely available under the GPL License. I have also been working with MySQL from version 4 (and that’s around 6 years now) so by now I am quite used to it. If you would like to install MySQL you can find it here: MySQL Community Server

The Connection String

Every database has its own specific connection string, and since there are so many different databases out there, it’s impossible to remember each connection string by heart. That is why I use ConnectionStrings.com to refresh my memory. It’s a great resource with connection strings for probably every type of database you can think of.

For this example we will be using the standard connection string which assumes MySQL is accessible on the default port of 3306. You can check out other MySQL connection strings here: Connection strings for MySQL

SERVER=myServerAddress; DATABASE=myDataBase; UID=myUsername; PWD=myPassword;

So, if MySQL was installed locally, and we had a database called testdb, and a MySQL database user called testuser with password testpass, the connection string would look like this:

SERVER=localhost; DATABASE=testdb; UID=testuser; PWD=testpass;

[continue reading…]

4 comments

New ASCII Codes Page

String Operations

I have added a new page to this blog called ASCII Codes, which can be accessed from the navigation bar at the top of the page. This new page is intended as a reference guide which you can use to look up an ascii code’s decimal, hexadecimal, and html values. There is also a description of each code and obviously the symbol represented by each ascii code.

Please feel free to use this page for what it is intended to be – your ascii reference guide.

Dave

1 comment