Strange problem when doing databinding to a combo box - Please Help

C

CGuy

URGENT HELP REQUIRED FROM GURUS

Hi,

I have a custom object that implements ICollection and IListSource. This
object has also an enumerator defined for it which implements IEnumerator
and IList. Now, I'm trying to bind object to a combo box. The bidning
happens correctly, but I'm unable to set the DisplayMember property of the
combo box (the count of items in the combo box gives me the correct value,
but no items are displayed in the combo box).

This is what I do.

My custom Class defenition

public class CustomCollection : ICollection, IListSource
{
...
...
/// <summary>
/// Returns an IList used for Data Binding
/// </summary>
/// <returns>IList</returns>
IList IListSource.GetList()
{
return(this.GetList());
}

/// <summary>
/// Returns an IList used for Data Binding
/// </summary>
/// <returns>CustomEnumerator</returns>
public CustomEnumerator GetList()
{
return(new CustomEnumerator(this, this.dataTable.DefaultView));
}

public class CustomEnumerator : IEnumerator, IList
{
...
...
}
}

This CustomCollection is a collection of customobjects (the enumerator
returns a custom object).

Now, in my windows form, I have the following code

CustomCollection custom = new CustomCollection();
custom.Fill(); // This fills the values
this.comboBox1.DataSource = custom;
this.comboBox1.DisplayMember = "Name"; //The field property of the object
that I want to be displayed
this.ComboBox1.ValueMember = "ID";

When the above code is executed, the combobox loads 10 entries (correct
value) but nothing is shown in the combobox (I guess because the DisplayName
property is not resolved correctly). How do I set the display name property
in this case? For example, if I do the following

MessageBox.Show(((customobject)comboBox1.Item[0]).Name) - gives me the value
of the Name property of the first "customobject" object in the combo box.
How do I set the DisplayName property of the combobox to this "Name"
property?


The strangest thing - if I set the Sorted property of the combo box to
"true", then the binding work fine!!! But the problem is that only the text
items are sorted and the associated value members are not sorted, so
everything gets messed up and I cannot rely on the SelectedValue property
anymore.

The same scenerio works perfectly in ASP.NET - if I bind my custom object to
a DropDownList webcontrol, and set the DataTextField property to "Name", it
works fine.

Thanks,
CGuy
 
M

M Hyde

I could be wrong but I believe you need to override the
ToString method of your object who's value you are trying
to display.
 

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