ComboBox databinding / DisplayMember

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm having problems getting the correct item to display in a bound combobox
in my WinForms app. Using the following code:

With Me.MyComboBox
.ValueMember = "MyIdField"
.DisplayMember = "ToString"
.DataSource = MyDataTable
End With

'MyDataTable is a table in the MyExampleDataSet, which I have the following
user code in:
Partial Class MyExampleDataSet

Partial Class MyDataTable_GetAllRow

Public Overrides Function ToString() As String
'return Id and Description e.g. 12 - Computer Books
Return String.Format("{0} - {1}", Me.MyIdField,
Me.MyDescriptionField)
End Function

End Class
End Class

The problem is that the ToString method is never called when databinding, so
the ComboBox only displays "System.Data.DataRowView" for each item. I would
like it to display items such as:
12 - Computer Books
14 - Cookery Books
etc.

What am I missing here?

thanks

Richard
 
Richard,

DisplayMember expects a field (property) name, therefore your overriden
method (ToString()) is never called. I would suggest you create a calculated
column on your table, and use it to display - similar to the following:

MyDataTable.Columns.Add("DisplayColumn", GetType(String),
"Convert(MyIdField, System.String) + ' - ' + MyDescriptionField")
....
Me.MyComboBox.DisplayMember = "DisplayColumn"
 
Hi Richard,

Yes, just as Sergey pointed out, the Combobox is performing complex
databinding with the DataTable. Combobox.DisplayMember and ValueMember both
require the "ColumnName" of the DataTable, you can not specify a method
name for it.

I think the solution provided by Sergey should be reliable. This method
created a separate Expression Column. This Expression Column allows you to
associate the values of other 2 properties/columns.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Can't you just do the formatting in the Select sentence?

SELECT SomethingHere, CStr(Idx) + " - " + Description AS MyDescription,
SomethingElseHere FROM Books WHERE....

And then set MyComboBox.DisplayMember = "MyDescription"

Good luck,
Johnny J.
 
Hi

Thanks everyone for your answers. I didn't realise that the DisplayMember
could only be set to a property name. In fact, maybe the confusion arose
because elsewhere in my app, the following works fine:

With Me.MyComboBox
..ValueMember = "Value"
..DisplayMember = "ToString"
..DataSource = MyArray
End With

In this case, MyArray is an array of a fairly simple class:

Public Class StringListItem
Public Text as string
Public Value as Integer

Public Overrides Function ToString() As String

Return String.Format("{0} - {1}", CStr(Value), Text)

End Function

End Class

When databinding occurs for this control, the ToString method IS called! If
I do not specify the DisplayMember, only the Value is displayed in the
ComboBox.

Sergey's method works fine - I added the calculated column once I had
retrieved the data into the DataTable.

Having got that working, I thought it would be better if I could add this
column via the DataSet designer, so keeping this code out of my presentation
layer. So I added the column to the DataTable, set the Expression in the
properties window. Previewing the data was fine - saw the new column with the
data as expected. However, when I set the DisplayMember to this column and
databound to the ComboBox, nothing was displayed. Looking at values for the
rows in the DataTable showed that the calculated column was returning DBNull.
Any idea why this could be? Strange that the data previews ok in the
designer, yet gives a different result in code!
Can't you just do the formatting in the Select sentence?
- I could do the formatting in the Stored Procedure, yes, however I believe
it is better practice to do this sort of thing in the application. This is
only one of many more complex examples in my app.

Sorry for the long post!

thanks

Richard
 
You have two fundamentally different situations here, but the .NET
framework has hidden the differences from you.

In the case that works, you are binding the combobox to an array. Each
item in the array is an instance of the class StringListItem, which
has properties Value and ToString. Therefore it works fine to use
those names in ValueMember and DisplayMember.

In the datatable case, when you bind the datatable to the combobox you
aren't really binding to the datatable itself - behind the scenes .NET
is binding the combobox to a list of DataRow objects in a DataView
created from your DataTable. The ValueMember and DisplayMember
properties of the combobox must refer to properties of that DataRow,
not the DataTable.
 
Hi Richard,

Thank you for the feedback.

Jack has explained to you why binding to "Array" will work with setting to
"Value" and "ToString". I would like to help you on the last DataSet
designer not working issue.

If I remember correct, I have ever got this working in a VS.net2003
project. Is it possible for you to provide a little sample project to
demonstrate this problem? I will help you to give it a research. You may
attach the sample project in a further reply as attachment(through Outlook
Express) or you may send it to me at: (e-mail address removed)(revmoe
"online.")

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Richard,

Since Jeffrey will not be in office for several days, would you please also
CC me when you send the sample project? Thank you for the trouble.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top