Madhawa Learns To Blog

.net, c#, sql, OOAD and more mad memory dumps...

Wednesday, May 04, 2005

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.

4 Comments:

At 6:09 PM , Blogger Mahasen said...

As you know… I’m in to performance these days. So prefer one large assembly than several small ones. It saves security checks, boundary crossing etc, and you’ll enjoy compiler optimizations such as inlining…

 
At 6:23 PM , Blogger Madhawa said...

yeah... I read that in that MSDN article :D, (that article really rocks man)
But in this case actually I'm trying to build an assembly that we can use in any project as a utility library which contains common methods we can use anywhere.

 
At 2:42 AM , Anonymous Anonymous said...

That was super helpful. Thanks!!!

 
At 1:21 PM , Blogger Unknown said...

- Tai nhac chuong hay mien phi cho dien thoai, tuyen chon nhac chuong hay nhat, DOC NHAT, kho tai nhac chuong mien phi lon nhat hien nay

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home