Is there a way to have a multiple field combo box using Microsoft Visual Studio with C#

Recommended Answers

All 5 Replies

Yes, and it's very easy too.
Say we have table Student (ID#, FirstName, LastName)

SqlDataSource.SelectCommand = "SELECT FirstName + ' ' + LastName AS FullName, ID FROM Student";
ComboBox.DataSource = SqlDataSource;
ComboBox.SelectedText = "FullName";
ComboBox.SelectedValue = "ID";
ComboBox.DataBind();

MJV,

Ramy has provided the excepted and standard way to do this.
If you are looking for something a little more extreme, then check out :
http://www.codeproject.com/KB/combobox/multicolumncombo.aspx

You may also want to explore the abilty to store tons of data to be associated with each combobox item. The item is of type Object. This means that you can create a class with all kinds of properties (even an entire dataset, datatable, datarow, etc. in this class and then add that as the Item to the combobox. The only requirement is that the class has an override ToString() method that will return the text you want displayed in the combobox and its dropdown.
Anytime you want to get at the underlying data of the item, just cast it to the type you assigned, and you have everything you want.

Hope this helps,
Jerry

Jerry I don't agree with you to override ToString method for such reason.
Let the ComboBox SelectedValue, SelectedText properties assigned to some Class properties that's better. http://fci-h.blogspot.com/2008/04/how-to-bind-combobox-to-array-of-object.html

Ramy,
Good discussion.
In most books that I have, the author (and this includes Microsoft best practices), any class should contain an override to the ToString method.

For example, in your blog posting there are a couple things to point out (please understand I am not trying to be critical, you are a great programmer and a wonderful help on this forum).
The first item is the comboBox1.Items.AddRange(students); line. It is not required, and does not do anything for the component because the items are automatically loaded from the DataSource property. IOW, you can comment out that line with no effect on the result.
The second item is when you do not have the ToString() override, and you request the string of the currently selected item you will get
"WindowsFormApplication1.Student" instead of the expected Text string.

MessageBox.Show( comboBox1.items[0].ToString() );

So, you can still assign the DisplayMember = "FullName"; and that works great, but when you override the Student.ToString() it is a little safer (show that in a moment), and it will always return the expected value.

public override string ToString()
        {
            if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName))
                return FullName;
            else
                return base.ToString();
        }

The student class does not contain any null-able properties, so this simple class would work fine, however if there are null-able objects that are used in the display, you could end up with an exception.

Once the ToString() method is overridden, then comboBox1.SelectedItem.ToString() cannot throw an exception, and will always return the correct Text.

What I was trying to point out to the original poster is that if the class contains many properties, and you need access to them, then you can do something like this:
(Lets say that we have a DataRow property in the Student class, and the constructor takes it as a parameter, or is loaded by the constructor)

Student stu = comboBox1.SelectedItem as Student;
if(stu != null)
{
    DataRow row = stu.Data;
    MessageBox.Show( row["Address"].ToString() );
   // other properties in Student 
   MessageBox.Show( stu.GirlFriendsPhone +"\r\n" + stu.DOB.ToString() );
}

// Jerry

Jerry I don't agree with you to override ToString method for such reason.

I don't show any objection to override ToString but I don't override it to bound it to Control. Any control bound data it should know what to display and what to represent as value.
1- Yes, you've right I wrote two statement which one is enough
2-

The student class does not contain any null-able properties, so this simple class would work fine, however if there are null-able objects that are used in the display, you could end up with an exception.

Once the ToString() method is overridden, then comboBox1.SelectedItem.ToString() cannot throw an exception, and will always return the correct Text.

You can handle it in its properties too, Jerry like what you write in ToString implementation you can right it too in any property.

Thanks Jerry for the great reply, it's so valuable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.