1

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

John Taylor
  • 131
  • 2
  • 3
  • 16
  • Please show the code where you build your datasource and how you connect it to the combobox1 – Steve Jun 09 '12 at 14:39

4 Answers4

2

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";
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    @Johnny write this where you assign datasource to your combobox – Sergey Berezovskiy Jun 09 '12 at 16:53
  • what is responsible of distance between rows? – John Taylor Jun 09 '12 at 17:04
  • @Johnny `{0,5}` says that first argument (id) should take at least five symbols (adds spaces at the beginning of string). Also you are free to change string format. – Sergey Berezovskiy Jun 09 '12 at 17:07
  • @Johnny looks like you didn't provide minimum length for argument in second case. See string formatting examples here http://msdn.microsoft.com/en-us/library/fht0f5be.aspx – Sergey Berezovskiy Jun 09 '12 at 18:30
  • 1
    All ok, but now i cant correct add rows in the table, cause parameter get value from combobox like a: combobox1.Text, this is number+name in 1 column, but i need in separate. I'm try use two textbox with bindingsource, when Form loading tb's get values, but when i change new double combobox, nothing... – John Taylor Jun 09 '12 at 22:37
2

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#)?

Community
  • 1
  • 1
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
1

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;}
 } 
anouar.bagari
  • 2,084
  • 19
  • 30
0

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188