thinzar
thinzar

Reputation: 1550

How to bind combobox to linq result value only?

I want to bind combobox to value only and distinct name .But I get like this:

enter image description here

I want only value eg;

Ice Box Handle,Green

Please answer to me.My code is like this

DataTable acc= accessory.GetData();

var query = (from t in acc.AsEnumerable()

select new {

           name = t["type"].ToString(),

           color = t["color"].ToString()

          }).Distinct().ToList();

cmbAccessoryName.DataSource = query;

Upvotes: 0

Views: 3233

Answers (1)

danyolgiax
danyolgiax

Reputation: 13086

Use ValueMember and DisplayMember property! to display inside the combo and use .Distinct() to filter duplicates!

try:

DataTable acc= accessory.GetData();

var query = (from t in acc.AsEnumerable()

select new {

       description=string.Format("{0},{1}",t["type"].ToString(), color = t["color"].ToString()) 

      }).Distinct().ToList();
cmbAccessoryName.DataSource = query;

Upvotes: 1

Related Questions