473,756 Members | 4,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 MyExampleDataSe t, which I have the following
user code in:
Partial Class MyExampleDataSe t

Partial Class MyDataTable_Get AllRow

Public Overrides Function ToString() As String
'return Id and Description e.g. 12 - Computer Books
Return String.Format(" {0} - {1}", Me.MyIdField,
Me.MyDescriptio nField)
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.Da taRowView" 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 4440
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.Col umns.Add("Displ ayColumn", GetType(String) ,
"Convert(MyIdFi eld, System.String) + ' - ' + MyDescriptionFi eld")
....
Me.MyComboBox.D isplayMember = "DisplayCol umn"

"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 MyExampleDataSe t, which I have the following
user code in:
Partial Class MyExampleDataSe t

Partial Class MyDataTable_Get AllRow

Public Overrides Function ToString() As String
'return Id and Description e.g. 12 - Computer Books
Return String.Format(" {0} - {1}", Me.MyIdField,
Me.MyDescriptio nField)
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.Da taRowView" 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.Displa yMember 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,
SomethingElseHe re FROM Books WHERE....

And then set MyComboBox.Disp layMember = "MyDescript ion"

Good luck,
Johnny J.

"Richard Bysouth" <sl***@nospam.n ospamwrote in message
news:85******** *************** ***********@mic rosoft.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 MyExampleDataSe t, which I have the
following
user code in:
Partial Class MyExampleDataSe t

Partial Class MyDataTable_Get AllRow

Public Overrides Function ToString() As String
'return Id and Description e.g. 12 - Computer Books
Return String.Format(" {0} - {1}", Me.MyIdField,
Me.MyDescriptio nField)
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.Da taRowView" 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(thro ugh Outlook
Express) or you may send it to me at: je***@online.mi crosoft.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
7686
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 different record, any changes in the current record are saved to an Access Database, the DataTable's Row Collection is Cleared and the New Record is read into the DataTable. (This is done to ensure that when the User goes to view a record, they get...
2
3468
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 a new login in the form loginaccount which create a new login and give me a dataset (created by class login), which I can see in my datagrid. But I want to fill up my combobox with only the name of the logon and I do this with an foreach. But when...
8
1982
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, DataAdapter and DataSet datasource. Each CB has it's own DA and DS. When I tried to use the same DS as the datadource for each of the five CB's, changing one of the CB's selection affectd the other four CB's. New business requirements require I...
6
1385
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 item in the rows manually. Just thought it would be nice to do it properly for a change. Here's the bit of code I experiment with: ============================================================================ ======================== Private Sub...
3
7438
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 information: Cannot bind to property or column FullName on DataSource. This error occurs with the databindings. Here is the code: --
5
3190
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 the value and displaymember properties. This works fine. My combobox contains the correct data. However I'm also binding my SelectedValue property to a column in a datatable. Here's where I'm lost. The combobox doesn't show the correct
6
2883
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
3
5320
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 OnPropertyChanged, but then the combobox does not display the right displaymember. Below is my sample code (it is working, when you paste it in a new windowsapplication, in a form and delete some generated code, but it is not a useful program but...
1
4250
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 object when text is deleted. However bound object contains old value in this case. To reproduce: 1. Run code
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.