473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataBinding multiple properties to a combobox

When i attempt to bind to the "Text" and "Value" property of a combobox
on a windows form the value is reset when I leave the combobox. The
comboboxes contain the correct Text and Values. I know this as the Value
property binds correctly on it own. It is only when I bind the "Text"
and "Value" that the issue occurs.

The following sample code includes my custom classes and the Form Clode.
I have several comboboxes and customer classes used as properties in the
Product class.
public class Product
{
private Customer _cust;
public Customer Customer
{
get{return _cust;}
set{_cust = value;}
}
....
}
public class ProductList : List<Product>
{
}

public class Customer
{
private int _id;
public int Id
{
get{return _id;}
set{_id = value;}
}

private string _name;
public string Name
{
get{return _name;}
set{_name= value;}
}
....
}
public class CustomerList : List<Customer>
{
// code to get list of customers
}

public class Form1 : Form
{
private CustomerList _custList;
private ProductList _prodList;
...

public void Form1()
{
InitializeCompo nents();

this.cboCustome rs.DataSource = _custList;
this.cboCustome rs.DisplayMembe r = "Name";
this.cboCustome rs.ValueMember = "Id";

_prodList = new ProductList;
_prodList.Add(n ew Product());

BindingManagerB ase bm = this.BindingCon text[_prodList];
bm.Position = 0;

this.cboCustome rs.DataBindings .Add("Text", _prodList,
"Customer.Name" );
this.cboCustome rs.DataBindings .Add("Value", _prodList,
"Customer.I d");
}
}

Thanks...
Jan 17 '08 #1
7 13519
I think you are double-binding it. Take out the lines where you add the
databindings, and just keep the ones where you set the data source, value
member, and display member.

RobinS.
--------------------------------------
"JTC^..^" <da********@jaz zthecat.co.ukwr ote in message
news:Xn******** *************** ***********@216 .196.109.145...
When i attempt to bind to the "Text" and "Value" property of a combobox
on a windows form the value is reset when I leave the combobox. The
comboboxes contain the correct Text and Values. I know this as the Value
property binds correctly on it own. It is only when I bind the "Text"
and "Value" that the issue occurs.

The following sample code includes my custom classes and the Form Clode.
I have several comboboxes and customer classes used as properties in the
Product class.
public class Product
{
private Customer _cust;
public Customer Customer
{
get{return _cust;}
set{_cust = value;}
}
....
}
public class ProductList : List<Product>
{
}

public class Customer
{
private int _id;
public int Id
{
get{return _id;}
set{_id = value;}
}

private string _name;
public string Name
{
get{return _name;}
set{_name= value;}
}
...
}
public class CustomerList : List<Customer>
{
// code to get list of customers
}

public class Form1 : Form
{
private CustomerList _custList;
private ProductList _prodList;
...

public void Form1()
{
InitializeCompo nents();

this.cboCustome rs.DataSource = _custList;
this.cboCustome rs.DisplayMembe r = "Name";
this.cboCustome rs.ValueMember = "Id";

_prodList = new ProductList;
_prodList.Add(n ew Product());

BindingManagerB ase bm = this.BindingCon text[_prodList];
bm.Position = 0;

this.cboCustome rs.DataBindings .Add("Text", _prodList,
"Customer.Name" );
this.cboCustome rs.DataBindings .Add("Value", _prodList,
"Customer.I d");
}
}

Thanks...
Jan 18 '08 #2
Hi JTC

What are you trying to accomplish?

Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. Move your
databinding to the Load event or later.

A ComboBox does not have a Value property. Do you mean SelectedValue?

Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 18 '08 #3


"Morten Wennevik [C# MVP]" <Mo************ @hotmail.comwro te in message
news:3E******** *************** ***********@mic rosoft.com...
Hi JTC

What are you trying to accomplish?

Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. Move your
databinding to the Load event or later.

A ComboBox does not have a Value property. Do you mean SelectedValue?

Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]

No value member ??? Are you sure ??? I think every combo has a text and a
value member ...

I think the solution could be something like this:

InitializeCompo nents();

this.cboCustome rs.DataSource = _custList;
this.cboCustome rs.DisplayMembe r = "Name";
this.cboCustome rs.ValueMember = "Id";

this.cboCustome rs.DataBind();

Keep in mind that the binding should be done only if IsPostBack property is
false, or you'll lose your viewstate !

HTH
--
Gianluca Gravina
http://blogs.ugidotnet.org/thinkingingrava

Jan 18 '08 #4
On 18 Jan, 09:34, Morten Wennevik [C# MVP]
<MortenWenne... @hotmail.comwro te:
Hi JTC

What are you trying to accomplish? *

Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. *Move your
databinding to the Load event or later.

A ComboBox does not have a Value property. *Do you mean SelectedValue?

Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]
My original plan was to use the bind to the SelectedItem, but it gives
error "Cannot Format the value of the desired type". The summaries for
SelectedItem, SelectedText and SelectedValue show these properties are
not available until runtime. I cannot find any other property to bind
to other than Text and Value.

(BTW i'm using .Net Framework 3.0)
Jan 18 '08 #5
On 18 Jan, 09:34, Morten Wennevik [C# MVP]
<MortenWenne... @hotmail.comwro te:
Hi JTC

What are you trying to accomplish? *
I need the ID and Name of the customer bound to the Product object.
>
Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. *Move your
databinding to the Load event or later.
Have done this without success.
A ComboBox does not have a Value property. *Do you mean SelectedValue?
You are correct, No it doesn't... Actually I'm using an 3rd party
control from infragistics which does. This has pointed me to the
source of the issue, (Thanks for the pointer)

Using the Windows native combobox I *can* bind to the "SelectedIt em"
property. With the Infragistics controls I can't, I get "Cannot Format
the Value of the desired type". I'm off to get support from
Infragistic now, unless someone here can help, it will be much
welcome.
>
Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 18 '08 #6
In case of a format mismatch you could try handling the Binding.Format/Parse
events to check the types it sends and expects.

The "selected.. ." properties are not availble until runtime because there is
nothing selected until you run the program, but you can still create
DataBinding against those properties.

cboCustomers.Da taBindings.Add( "SelectedIt em", _prodList, "Customer", true);
cboCustomers.Da taBindings["SelectedIt em"].Format += new
ConvertEventHan dler(customer_F ormat);
cboCustomers.Da taBindings["SelectedIt em"].Parse += new
ConvertEventHan dler(customer_P arse);

....

void customer_Parse( object sender, ConvertEventArg s e)
{
// e.DesiredType is the type it expects
// e.Value contains the type it is given
// replace e.Value for the correct type
}

void customer_Format (object sender, ConvertEventArg s e)
{
// e.DesiredType is the type it expects
// e.Value contains the type it is given
// replace e.Value for the correct type
}

I have seen this error before where DesiredType and e.Value type was
identical and it would throw an exception if parse/format were not handled,
but it would not update values if parse/format were handled.

I notice there is a new ComboBoxItem class available for .Net 3.0 which is
not in .Net 2.0 so there may be some changes regarding the ComboBox
behaviour. However, I do not have access to .Net 3.0/3.5 at the moment so I
can't check it out.
--
Happy Coding!
Morten Wennevik [C# MVP]
"JTC^..^" wrote:
On 18 Jan, 09:34, Morten Wennevik [C# MVP]
<MortenWenne... @hotmail.comwro te:
Hi JTC

What are you trying to accomplish?

Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. Move your
databinding to the Load event or later.

A ComboBox does not have a Value property. Do you mean SelectedValue?

Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]

My original plan was to use the bind to the SelectedItem, but it gives
error "Cannot Format the value of the desired type". The summaries for
SelectedItem, SelectedText and SelectedValue show these properties are
not available until runtime. I cannot find any other property to bind
to other than Text and Value.

(BTW i'm using .Net Framework 3.0)
Jan 18 '08 #7
You are correct, No it doesn't... Actually I'm using an 3rd party
control from infragistics which does. This has pointed me to the
source of the issue, (Thanks for the pointer)
Ah, a vital piece of information.

Infragistics' ComboBox (UltraCombo) does indeed have a Value property and in
case of Infragistics controls the Text property is merely used for displaying
the Value property. I think in case of the UltraCombo control, the Value
property may behave like SelectedValue. If it doesn't try binding against
SelectedRow and use the Parse/Format events to retrieve the
SelectedRow.Lis tObject property which should hold the Customer class. Or
maybe you can even bind against SelectedRow.Lis tObject directly.

--
Happy Coding!
Morten Wennevik [C# MVP]
"JTC^..^" wrote:
On 18 Jan, 09:34, Morten Wennevik [C# MVP]
<MortenWenne... @hotmail.comwro te:
Hi JTC

What are you trying to accomplish?

I need the ID and Name of the customer bound to the Product object.

Creating Databinding in the constructor is generally a bad idea as the
controls may not have been properly created at this time. Move your
databinding to the Load event or later.

Have done this without success.
A ComboBox does not have a Value property. Do you mean SelectedValue?


Using the Windows native combobox I *can* bind to the "SelectedIt em"
property. With the Infragistics controls I can't, I get "Cannot Format
the Value of the desired type". I'm off to get support from
Infragistic now, unless someone here can help, it will be much
welcome.

Try to bind the actual selected Customer using SelectedItem instead of the
displayed text which is not guaranteed to be related to the underlying
Customer

this.cboCustome rs.DataBindings .Add("SelectedI tem", _prodList, "Customer") ;

--
Happy Coding!
Morten Wennevik [C# MVP]

Jan 18 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1971
by: Paul Fairless | last post by:
Customers table - contains Columns: CustID, Surname, Forename, TtlID Titles table - contains Columns: TtlID, Title TtlID is a Foreign Key in the Customers table. I have a Form frmCustomers which contains a ComboBox to select the Customer's Title. The ComboBox is populated from the database
15
3666
by: Tim Jarvis | last post by:
Hi, I have an object that I am binding to a text box, this object exposes a boolean field, and I have implemented a format event handler and a parse event handler for the binding object, where I convert the bool value to some meaningful text. i.e. Binding b = new
1
3616
by: Gary Shell | last post by:
I have a pair of combo boxes on a form. Both have their SelectedValue property bound to a column on a table called "Input_Output". One column is called "Class" and the second is called "SubClass". Each combobox has its datasource, displaymember and SelectedValue member bound to separate tables thru individual datatsets thru individual data...
0
1001
by: Michael | last post by:
Hi everyone, I'm working on cutting down code in a form I'm working on, and decided to bind the controls to a dataset. I have two problems now. I have a couple datasets that I'm binding to. 1. I have all databinding properties set for the control. When I fill the dataset, there is now data showing up in the control. I'm using this code to...
3
7749
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on the form. (It's a Company -> People -> Personal Data relationship) When I bind my datasource to the ListBox, I find that the
2
1356
by: cbrown | last post by:
I am binding a custom collection to a combobox, which gives me no errors, but displays PintClub.Member for each entry. How can I set the displaymember and valuemember fields correctly. Do I need to inherit anything in my collection for this. The collection "MemberCollection" contains "Member" objects with public properties available. ...
5
3174
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...
5
1814
by: Dennis | last post by:
I am trying to create a form using databinding to a dataset and one of the fields requires the user to select from a list of optons. Any hints on how to do this other than bind the field to a label or text box to display the current field value then add a combo box with the list of items to chose from? -- Dennis in Houston
6
5442
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class type constraint 'System.Windows.Forms.ComboBox' must come before any other constraints If I switch place of ComboBox with TextBoxBase, it would...
0
7512
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...
0
7438
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...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
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...
0
6036
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...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
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...
1
1051
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.