문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top