By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

ComboBox databinding / DisplayMember

P: n/a
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
Jul 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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"

"Richard Bysouth" wrote:
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
Jul 19 '07 #2

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '07 #3

P: n/a
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.

"Richard Bysouth" <sl***@nospam.nospamwrote in message
news:85**********************************@microsof t.com...
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

Jul 19 '07 #4

P: n/a
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: je***@online.microsoft.com(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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 23 '07 #5

P: n/a
Hi Jeffrey

I'll send you a sample project next week.

thanks

Richard
Jul 23 '07 #6

P: n/a
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 (wa****@online.microsoft.com, 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.

Jul 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.