Why Is My Combobox So Slow When Databinding?

While working on a project for ImageSource, I recently found an interesting quirk when using the .NET Windows form combobox control.  The issue occurred when I was populating the combobox’s item list with a datasource.  The data was populating at a very slow rate, slower than it was taking to retrieve the data I was using to populate it from the database.  The code I was using looked something like this:

Dim tbl As DataTable = _dataSource.ReadAllItemsFromDbTable()
cboComboBox.DataSource = tbl
cboComboBox.DisplayMember = "Name"
cboComboBox.ValueMember = "Value"

What I later found out was that the ordering of the above code made all the difference, and that by setting the DisplayMember property after setting the DataSource property, I was causing the combobox to reload all of the item data a second time.  To stop it from doubling all of the work, I just had to make a simple code change.  This involved moving the setting of the DisplayMember and ValueMember properties above the setting of the DataSource property.

Dim tbl As DataTable = _dataSource.ReadAllItemsFromDbTable()cboComboBox.DisplayMember = "Name"
cboComboBox.ValueMember = "Value"
cboComboBox.DataSource = tbl

This fixed the issue, as the DisplayMember property is now already set before all of the items are loaded.  I hope this helps fix any issue you may have had with this control.


2 thoughts on “Why Is My Combobox So Slow When Databinding?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s