0

I have the following code :

private void LoadCombos()
{
    //
    //Entity
    cmbEntity.ValueMember = "ID";
    cmbEntity.DisplayMember = "Name";
    cmbEntity.DataSource = store.Entities; //store is an objectContext
    //
}

I am trying to display Name as well as ID using this :

cmbEntity.DisplayMember = "ID+Name";

Is there a way by which I can achieve this, so as to display ID as well as name in combobox?

leppie
  • 115,091
  • 17
  • 196
  • 297
Prakash Vishwakarma
  • 814
  • 2
  • 9
  • 25

2 Answers2

2

Try this:

cmbEntity.DataSource =
             store.Entities
                     .ToList()
                     .Select(e => new {Id = e.Id, Name = e.Id + e.Name});

or you can use

cmbEntity.DataSource =
     store.Entities
          .Select(e => new {Id = e.Id,
                     Name = SqlFunctions.StringConvert((decimal)e.Id + e.Name});

In this case you must add reference to assembly System.Data.Entity.dll and import namespace System.Data.Objects.SqlClient.

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

Similar to this question is already answered here containing one of the ways you can achieve it using Dictionary

var dict = new Dictionary<Guid, string>();
foreach (var row in dt.Rows)
{
    dict.Add(row["GUID"], row["Name"] + " " + row["Surname"]);
}
cbo.DataSource = dict;
cbo.DataTextField = "Value";
cbo.DataValueField = "Key";
cbo.DataBind();
Community
  • 1
  • 1
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50