468,535 Members | 1,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,535 developers. It's quick & easy.

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

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
Jul 19 '05 #1
0 2590

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by amar shukla | last post: by
5 posts views Thread by Dennis | last post: by
2 posts views Thread by sara | last post: by
3 posts views Thread by Tomasz J | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.