471,337 Members | 988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Show code and name in combobox picklist

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<ProductprodList = 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());
}
}

Jun 27 '08 #1
4 2096
On May 28, 8:09*am, "Andrus" <kobrule...@hot.eewrote:
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.
Jun 27 '08 #2
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?

Jun 27 '08 #3
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.

Jun 27 '08 #4

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<ProductprodList = 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());
}
}

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Idelso Quintero | last post: by
12 posts views Thread by Mike Brashars | last post: by
8 posts views Thread by gumi | last post: by
1 post views Thread by Andrus | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.