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

DataBinding multiple properties to a combobox

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

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

P: n/a


"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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.