Show code and name in combobox picklist

A

Andrus

I need select from list containing product code and name but show only code
in combo textbox.

Tried code below but dropdown list shows only code.
How to display code AND name in selection list ?

Andrus.


using System.Collections.Generic;
using System.Windows.Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox() { DisplayMember = "DisplayMember",
ValueMember = "Code" };
List<Product> prodList = new List<Product>();
prodList.Add(new Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(new Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings.Add("SelectedValue", prodList, "Code");
Controls.Add(cm);
}

class Product
{
public ComboBox Cb;
public string Code { get; set; }
public string Name { get; set; }

public string DisplayMember
{
get
{
if (Cb.DroppedDown)
return Code + " " + Name;
else
return Code;
}
}
}

static void Main()
{
Application.Run(new testForm());
}
}
 
I

Israel

How to display code AND name in selection list ?

You need to override the ToString() for the Product class to display
it the way you want to see it in the combobox.
 
I

Ignacio Machin ( .NET/ C# MVP )

I need select from list containing product code and name but show only code
in combo textbox.

Tried code below but dropdown list shows only code.
How to display code AND name in selection list ?

Andrus.

using System.Collections.Generic;
using System.Windows.Forms;

class testForm : Form
{
    testForm()
    {
        ComboBox cm = new ComboBox() { DisplayMember = "DisplayMember",
ValueMember = "Code" };
        List<Product> prodList = new List<Product>();
        prodList.Add(new Product() { Code = "001", Name = "Wine", Cb =
cm });
        prodList.Add(new Product() { Code = "002", Name = "Beer", Cb =
cm });
        cm.DataSource = prodList;
        cm.DataBindings.Add("SelectedValue", prodList, "Code");
        Controls.Add(cm);
    }

    class Product
    {
        public ComboBox Cb;
        public string Code { get; set; }
        public string Name { get; set; }

        public string DisplayMember
        {
            get
            {
                if (Cb.DroppedDown)
                    return Code + " " + Name;
                else
                    return Code;
            }
        }
    }

    static void Main()
    {
        Application.Run(new testForm());
    }



}- Hide quoted text -

- Show quoted text -

Hi,

Basically you have to concatenate both properties, you can do it in
one of two ways:
- Creating a new property that concatenate it:
public string XXX{ get{ return Code + " " + Name;}}
 
A

Andrus

Basically you have to concatenate both properties, you can do it in
one of two ways:
- Creating a new property that concatenate it:
public string XXX{ get{ return Code + " " + Name;}}

This is done already in my code !

I see only one way here.

Andrus.
 
J

Jeremy

This is a great learning example, so thanks for posting it. If anyone can
answer my questions, I'd be grateful.

I see that you're creating a combobox in code; my preference would be to
have this in the xaml. What is your reasoning?

When I try to assign to mycombo.DataSource, vs2k8 says comboboxes don't have
DataSource as member. What am I missing?
 
A

Andrus

I see that you're creating a combobox in code; my preference would be to
have this in the xaml. What is your reasoning?

When I try to assign to mycombo.DataSource, vs2k8 says comboboxes don't
have DataSource as member. What am I missing?

This is winforms, not wpf.

Andrus.
 
A

Andrus

Israel,
You need to override the ToString() for the Product class to display
it the way you want to see it in the combobox.

Thank you.
I tried code below but dropdown list shows only code.
How to force it to show both code and name ?

Andrus.

using System.Collections.Generic;
using System.Windows.Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox()
{
ValueMember = "Code"
};
Lis<Product> prodList = new List<Product>();
prodList.Add(new Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(new Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings.Add("SelectedValue", prodList, "Code");
Controls.Add(cm);
}

class Product
{
public string Code { get; set; }
public string Name { get; set; }

public override string ToString()
{
return Code + " " + Name;
}
}

static void Main()
{
Application.Run(new testForm());
}
}
 

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