Show code and name in combobox picklist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    #1

    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.Collecti ons.Generic;
    using System.Windows. Forms;

    class testForm : Form
    {
    testForm()
    {
    ComboBox cm = new ComboBox() { DisplayMember = "DisplayMember" ,
    ValueMember = "Code" };
    List<Productpro dList = new List<Product>() ;
    prodList.Add(ne w Product() { Code = "001", Name = "Wine", Cb =
    cm });
    prodList.Add(ne w Product() { Code = "002", Name = "Beer", Cb =
    cm });
    cm.DataSource = prodList;
    cm.DataBindings .Add("SelectedV alue", 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());
    }
    }

  • Israel

    #2
    Re: Show code and name in combobox picklist

    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.

    Comment

    • Jeremy

      #3
      Re: Show code and name in combobox picklist

      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.DataSou rce, vs2k8 says comboboxes don't have
      DataSource as member. What am I missing?

      Comment

      • Andrus

        #4
        Re: Show code and name in combobox picklist

        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.DataSou rce, vs2k8 says comboboxes don't
        have DataSource as member. What am I missing?
        This is winforms, not wpf.

        Andrus.

        Comment

        • Andrus

          #5
          Re: Show code and name in combobox picklist


          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.Collecti ons.Generic;
          using System.Windows. Forms;

          class testForm : Form
          {
          testForm()
          {
          ComboBox cm = new ComboBox()
          {
          ValueMember = "Code"
          };
          Lis<Productprod List = new List<Product>() ;
          prodList.Add(ne w Product() { Code = "001", Name = "Wine", Cb =
          cm });
          prodList.Add(ne w Product() { Code = "002", Name = "Beer", Cb =
          cm });
          cm.DataSource = prodList;
          cm.DataBindings .Add("SelectedV alue", 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());
          }
          }

          Comment

          Working...