none
Display two columns in a combo box RRS feed

  • Question

  • HI all,

    I am using c sharp and I am using binding source class. I want to display two fields in the combo box. I can do this with one field like , combobox.datasource = bindingsourcename and

    combobox.displaymember = "Type" (coloumn of the table)

    I want to display both type and title in this combo box. To do this is it need to desing a custom combo box? if so how to do this?

    thanx

     

    Tuesday, May 3, 2011 9:21 AM

Answers

  • E.g. you have class A with properties Type and Title and you use your clas A as a binding source:

     

    class A

    {

    public string Type {get; set;}

    public string Title {get; set;}

    }

     

    Create new class, e.g. "B" as the following:

     

    Class B

    {

      private A _a;

      public string StringToShow

      {

    get {retirn string.Format("{0} {1}", _a.Type, _a.Title);}

      }

      public B (A a)

      {

    _a = a;

      }

    }

     

    and use B as a datasource instead of A.

     

    Or, if it's possible, create new property in class A (the same like StringToShow in an example above)

    • Proposed as answer by eryang Tuesday, May 17, 2011 9:40 AM
    • Marked as answer by Creative_Pro Friday, May 20, 2011 4:17 AM
    Thursday, May 12, 2011 7:02 AM

All replies

  • Hi Anu,

    There is no direct way to achieve this. You have to create a custom combobox and override couple of methods. You can find some useful code below.

    http://www.codeproject.com/KB/cpp/multicolumncombo.aspx

    http://www.codeproject.com/KB/combobox/multicolumncombo.aspx


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, May 3, 2011 9:47 AM
  • There is another way: you can create a proxy-class that includes all table's columns and a custom property that returns a string that consist of two your columns.

    A list of these classes should be used as a binding source.

    Tuesday, May 10, 2011 12:26 PM
  • Hi Ekaterina,

    Could you please describe further on above solution. Thanks

    Wednesday, May 11, 2011 3:07 AM
  • E.g. you have class A with properties Type and Title and you use your clas A as a binding source:

     

    class A

    {

    public string Type {get; set;}

    public string Title {get; set;}

    }

     

    Create new class, e.g. "B" as the following:

     

    Class B

    {

      private A _a;

      public string StringToShow

      {

    get {retirn string.Format("{0} {1}", _a.Type, _a.Title);}

      }

      public B (A a)

      {

    _a = a;

      }

    }

     

    and use B as a datasource instead of A.

     

    Or, if it's possible, create new property in class A (the same like StringToShow in an example above)

    • Proposed as answer by eryang Tuesday, May 17, 2011 9:40 AM
    • Marked as answer by Creative_Pro Friday, May 20, 2011 4:17 AM
    Thursday, May 12, 2011 7:02 AM