http://madhawa.blogspot.com/2005_05_01_archive.html
Correct way to bind Windows Forms ListControls
Did you ever think there is a correct way to bind a windows
forms ListControl not to just setting DataSource, ValueMember and DisplayMember
in any order. If you bind that way ignoring the order SelectedIndexChanged and SelectedValueChanged
events will fire several times. And you can't make sure what will return for SelectedValue
property from DataRowView and Int32. I think you guys won’t argue that this is a
worst case.
Ok guys, there is a correct order to do that and then you won’t
get any troubles on that.
You have to set those properties in following order.
1. DisplayMember
2. ValueMember
3. DataSource
And you can make sure you and your colleagues wont do wrong
by using a helper function for that.
Well pretty good idea, ha. I went little further and create
that helper method like this.
public static void ListControlBinding( ref ListControl listControl,object
dataSource, string displayMember,string valueMember )
{
listControl.DisplayMember = displayMember;
listControl.ValueMember = valueMember;
listControl.DataSource = dataSource;
}
public static void ListControlBinding( ref ComboBox comboBox,object
dataSource, string displayMember,string valueMember )
{
comboBox.DisplayMember = displayMember;
comboBox.ValueMember = valueMember;
comboBox.DataSource = dataSource;
}
I have put those methods in a another library. That’s why I'm
passing Control as reference type. And I have overloaded ListControlBinding method
for ListControl as well as ComboBox.
You can get extended info about this problem here: http://www.codeproject.com/csharp/ScoMListControlBinding.asp