473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ComboBox databinding / DisplayMember

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
6 4414
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
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
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
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
Hi Jeffrey

I'll send you a sample project next week.

thanks

Richard
Jul 23 '07 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Timothy White | last post by:
I have a Windows Form Which Displays one record at a time. The DataTable object which is bound to the Form Controls only contains the record that is being displayed. When I need to display a...
2
by: Hugo Lefevre | last post by:
dear, I have a form (frmlogonaccounts.cs) with a combobox, which I want to fill up with the first column of a dataset. The dataset is created in the class Login. My problem is the next : I make...
8
by: Steve B. | last post by:
My Program: a local C# ADO.Net application using VS 2003 Right now I have about 5 ComboBox's which provides drop down selection entries from the same MS-Access table via a DataConnection,...
6
by: Dany P. Wu | last post by:
Hi everyone, As usual, weekend is tinkering time for students and I'm playing with combobox databinding for the first time. Previously I have always iterated through the records and added each...
3
by: Dan Slaby | last post by:
I have a webservice that I want to populate a combobox on a windows form. The webservice creates the correct XML output, but when I attempt to bind it to a combobox I get this error: Additional...
5
by: Peter M. | last post by:
I'm struggling with combobox databinding with something I consider a bug... I'm binding my combobox to an array of structs. The struct exposes two public properties, ID and Name, to be used as...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
3
by: Gerrit | last post by:
Hi, I try to learn programming in c# with databinding controls. Now I have a problem with a ComboBox with the advanced properties for databinding, I want to set the DataSourceUpdateMode to...
1
by: Andrus | last post by:
I need to enter null value from combobox to business object property. My combobox datasource does not contain ValueMember with null value. So I tried to create combobox which stores null to bound...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.