473,498 Members | 1,671 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()
{
InitializeComponents();

this.cboCustomers.DataSource = _custList;
this.cboCustomers.DisplayMember = "Name";
this.cboCustomers.ValueMember = "Id";

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

BindingManagerBase bm = this.BindingContext[_prodList];
bm.Position = 0;

this.cboCustomers.DataBindings.Add("Text", _prodList,
"Customer.Name");
this.cboCustomers.DataBindings.Add("Value", _prodList,
"Customer.Id");
}
}

Thanks...
Jan 17 '08 #1
7 13512
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********@jazzthecat.co.ukwrote 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()
{
InitializeComponents();

this.cboCustomers.DataSource = _custList;
this.cboCustomers.DisplayMember = "Name";
this.cboCustomers.ValueMember = "Id";

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

BindingManagerBase bm = this.BindingContext[_prodList];
bm.Position = 0;

this.cboCustomers.DataBindings.Add("Text", _prodList,
"Customer.Name");
this.cboCustomers.DataBindings.Add("Value", _prodList,
"Customer.Id");
}
}

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.cboCustomers.DataBindings.Add("SelectedItem", _prodList, "Customer");

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


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:3E**********************************@microsof t.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.cboCustomers.DataBindings.Add("SelectedItem", _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:

InitializeComponents();

this.cboCustomers.DataSource = _custList;
this.cboCustomers.DisplayMember = "Name";
this.cboCustomers.ValueMember = "Id";

this.cboCustomers.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.comwrote:
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.cboCustomers.DataBindings.Add("SelectedItem", _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.comwrote:
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 "SelectedItem"
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.cboCustomers.DataBindings.Add("SelectedItem", _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.DataBindings.Add("SelectedItem", _prodList, "Customer", true);
cboCustomers.DataBindings["SelectedItem"].Format += new
ConvertEventHandler(customer_Format);
cboCustomers.DataBindings["SelectedItem"].Parse += new
ConvertEventHandler(customer_Parse);

....

void customer_Parse(object sender, ConvertEventArgs 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, ConvertEventArgs 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.comwrote:
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.cboCustomers.DataBindings.Add("SelectedItem", _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.ListObject property which should hold the Customer class. Or
maybe you can even bind against SelectedRow.ListObject directly.

--
Happy Coding!
Morten Wennevik [C# MVP]
"JTC^..^" wrote:
On 18 Jan, 09:34, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.comwrote:
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 "SelectedItem"
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.cboCustomers.DataBindings.Add("SelectedItem", _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
1957
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...
15
3654
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...
1
3602
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"....
0
994
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...
3
7744
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...
2
1350
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...
5
3167
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...
5
1805
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...
6
5424
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...
0
7126
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,...
0
7210
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...
1
6891
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...
0
5465
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,...
1
4916
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...
0
4595
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...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.