John Taylor
John Taylor

Reputation: 143

How to add 2 column in combobox?

I think for displaying columns in combobox is responsible this string in Desingner of Form:

this.ComboBox1.DisplayMember = "name";

But, i cant add anything near "name", combobox shows in all strings : System.Data.DataRowView

Maybe its wrong, tell me please how do this?

I'm just drag table as combobox from Data Source:

When Form Loading:

private void frmCheck_Load(object sender, EventArgs e)
{        
 this.ttzTableAdapter.Fill(this.dbDataSet.ttz, Convert.ToInt32(idFromWork.Text));
}

So combobox show, just "name" in list, i want that he show "number" and "name" in list.

"Convert.ToInt32(idFromWork.Text)" 

parameter for show names where id_ttz(ID) = @idwork

Upvotes: 1

Views: 14168

Answers (4)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236288

DisplayMember is a string specifying the name of an object property that is contained in the collection specified by the DataSource property. You cannot specify several properties to display. If you'll try to do this, your 'compound property' will not be found, and objects will be displayed via ToString() implementation (thats why you see System.Data.DataRowView string).

If you need to display several properties of object, you can create custom multi-column combobox and implement it's pop-up as ListView or DataGridView.

UPDATE: Instead of binding directly to DataTable of DataView, you can create your own anonymous type, which will provide formatted text for displaying:

ComboBox1.DataSource = tdbDataSet.ttz.AsEnumerable()
          .Select(row => new 
               { 
                 Text = String.Format("{0,5} | {1}", row["id_ttz"], row["name"]),
                 Value = row["id_ttz"]
               })
         .ToList();

ComboBox1.DisplayMember = "Text";
ComboBox1.ValueMember = "Value";

Upvotes: 2

anouar.bagari
anouar.bagari

Reputation: 2104

In your query add a calculated column

  select name, ..., name+' '+number as colToDisplay...

and use it like this

   this.ComboBox1.DisplayMember = "colToDisplay";

Or if you are binding your combo to a custom object

add a new Property that combines the columns you need to display

 public string PropertyToShow
 {
   get{return name+" "+otherProp;}
 } 

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112632

You have several possibilities. If you don't specify a DisplayMember, the combo box uses the ToString method to get a string. So one option is naturally to override the ToString method of your class, if you are under control of it. Or you could add a property returning an appropriate string and use this in DisplayMember.

A completely different approach is to create your own combo box by deriving one from ComboBox. Then change the DrawMode of your combo box to OwnerDrawFixed if the items have all the same height or OwnerDrawVariable if they might have different heights. You will have to override OnDrawItem and do your own drawing logic. If you specified OwnerDrawVariable you must override OnMeasureItem as well in order to tell the combo box height of each item.

Using this second approach enables you to create real columns and to draw a vertical separator and much more. You can draw icons use different background and text colors, different fonts etc.

Upvotes: 0

Md Kamruzzaman Sarker
Md Kamruzzaman Sarker

Reputation: 2407

You can not add two column in combobox. how ever you can concat values from two object. see this WinForms combobox with multiple columns (C#)?

Upvotes: 2

Related Questions