Greetings!

From time to time, I receive emails from software developers around the world, specially from Middle East and Asia region, discussing some technical issue. So while I was answering an email today, I thought that it would be great if I create a category “Real World Scenario” on my blog and post the solution there so that many others could benefit.

But first please note some points 🙂

  • Being an MVP doesn’t mean that I know everything 🙂 but I will try my level best to help you in providing solution
  • The solution may not be the best approach to solve a particular problem or there may be a better way to code
  • Also, I may not post the exact question and code asked from person but will create question of similar situation otherwise I need to get permission from that person first

So this is my first post on the series of Real World Scenario.

Scenario: Binding a ComboBox to a Generic List of Custom class A with Display member as another class B exposed as public property in class A

Consider the following code snippet below:

public class B
{
    public string Info { get; set; }
    public int Id { get; set; }
}
public class A
{
    public string Name { get; set; }
    public B Information { get; set; }
}

We created two classes A and B and added some automatic properties in them. Most of the time, we want to bind our ComboBox (let’s say comboBox1) with List and Display member as some primitive data type, something like:

//populate some data
A obj1OfA= new A { Name = "Test1" , Information=new B { Info= "Info Test 1", Id = 1} };
A obj2OfA= new A { Name = "Test2" , Information=new B { Info= "Info Test 2", Id = 2} };
listOfA.Add(obj1OfA);
listOfA.Add(obj2OfA);
//and bind the comboBox1 with A
comboBox1.DataSource = listOfA;
comboBox1.DisplayMember = "Name";

In the code snippet above, we initialized a generic list of class A and then we created two objects of A using object intializer, a feature of C# 3.0, and finally added the objects in list along with binding of list to combobox.

So that will do our job because Name is a public property of type string in class A and it will display in Name easily. The question was that when you want to set Display Member as Information like:

comboBox1.DataSource = listOfA;
comboBox1.DisplayMember = "Information";

it will not display appropriate data in the combo box and that will be something like

WindowsFormsApplication1.B
WindowsFormsApplication1.B

and that is not what we wanted to display. The reason is that it actually calls ToString method and that returns type name in case when display member is Information. So the solution is that you need to override ToString() method in the class B and return the desired data that you want to display. So let’s go ahead and change our class B:

public class B
{
    public string Info { get; set; }
    public int Id { get; set; }
    public override string ToString()
    {
        return Info;
    }
}

and now when we execute our code we will get appropriate output showing the value of Info property in class B bind to our combobox 🙂 That’s all !

Thank you for the email sent by you, The Developers. Interaction with community really gives me an opportunity to learn from you people as well and a chance to serve the community. Thank you once again! Have a fantastic time!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *