Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top