DataBinding multiple properties to a combobox

J

JTC^..^

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...
 
R

RobinS

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.
 
M

Morten Wennevik [C# MVP]

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");
 
G

grava

Morten Wennevik said:
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");

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
 
J

JTC^..^

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");

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)
 
J

JTC^..^

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.
 
M

Morten Wennevik [C# MVP]

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.
 
M

Morten Wennevik [C# MVP]

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^..^ said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top