Data Binding a combo box to multiple fields

Data Binding a combo box to multiple fields

Old forum URL: forums.lhotka.net/forums/t/3869.aspx


dlabar posted on Thursday, November 08, 2007

I have a Customer CSLA Business Object that contains a Customer Name, and a Customer Id.  I'd like to be able bind both fields to a single ComboBox with the format "Customer Name - Customer Id" and I'm having some troubles.  I can add a new property to the Customer Business object that does this for me, but I doesn't really belong there.

I'm using .Net 2008 and was wondering if there was a possiblity of using Method Extensions.  Created one, but it is a method, and I can't data bind to a method.  Is there anyway to make an Property Extension?  I'm I missing something painfully obvious? 

Curelom replied on Thursday, November 08, 2007

Are you using WPF or Forms?  in WPF, you would just bind the object to the dropdown and use a template to display the data in the way you like.

dlabar replied on Thursday, November 08, 2007

I'm currently Using Forms.  There is nothing keeping me from switching to WPF.  Is that a major jump? 

DavidDilworth replied on Friday, November 09, 2007

Is the BO you are talking about a Name-Value List?    That is the CSLA class designed specifically for binding to ListBox style controls.

dlabar replied on Friday, November 09, 2007

No it is not.  90% of the time these BO are used for backend processing and they don't need these "User Friendly" display values. 

JBergdahl replied on Friday, November 09, 2007

Why not just override the ToString() function to return the string in a format you want?

Regards;
/jb

dlabar replied on Friday, November 09, 2007

I don't think it was possible to databind to ToString().  If there is a way, please let me know.

AAKLebanon replied on Saturday, November 10, 2007

in reality you can, this is an exampe:

Public Class BusinessObject

Public Overrides Function ToString() As String

Return "ToString"

End Function

End Class

Public Class BusinessObjectsList

Inherits System.ComponentModel.BindingList(Of BusinessObject)

End Class

Public Class Form1

Private list As New BusinessObjectsList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

list.Add(New BusinessObject)

list.Add(New BusinessObject)

BusinessObjectsListBindingSource.DataSource = list

End Sub

End Class

you must add a Combobox to your form and bind it to BusinessObjectList (by creating a new object data source from the Data sources pane.

now when you run the application, you will notice that the combobox is filled with 2 objects named ToString :)

 

dlabar replied on Monday, November 12, 2007

I think that is about as close as I can come given my current setup.  Thanks.

DavidDilworth replied on Friday, November 09, 2007

So I would suggest that the Use Case you have here is for a specific "user-friendly" string in a ListBox for a specific UI requirement. 

It's a different BO to the other one.

Use the NVL class, or something similar.

Curelom replied on Friday, November 09, 2007

dlabar:
I'm currently Using Forms.  There is nothing keeping me from switching to WPF.  Is that a major jump? 

It is a huge jump.  If your applicaiton is already using Forms, I wouldn't make the jump.  If you are just starting out and have some extra time(a lot of extra time) for the learning curve, it might be worth it.  In either case, I echo the other comments to this post of creating an nvl object for this use case.  You would generally use the nvl in WPF as well as it is more light weight.

Copyright (c) Marimer LLC