Faster databinding

My friend Harley Jácome has started blogging (hey, ¡está en español!). There he describes a neat Windows Forms databinding trick that is probably very old but that I wasn't aware of. Which one do you think is faster?

CitiesComboBox.DataSource = CitiesDataTable;
CitiesComboBox.ValueMember = "Id";
CitiesComboBox.DisplayMember = "Name";

Or

CitiesComboBox.ValueMember = "Id";
CitiesComboBox.DisplayMember = "Name";
CitiesComboBox.DataSource = CitiesDataTable;

It turns out that the later is faster. According to Harley's tests, more than twice as fast. The reason? When the combobox has a null datasource, changing ValueMember or DisplayMember do basically nothing but when it points to something, the same changes repopulate the combobox. Hence, the second option is more efficient. The funny thing is that, out of trying to be hierarchically organized, I always set the datasource first, silly me.

9 Comments

Comments have been disabled for this content.