user3319756 user3319756 - 4 months ago 17
SQL Question

How to Show multiple fields in combo box when using linq to sql

I have a database of rice types and prices. It has three fileds:

fld_RiceType fld_RiceId fld_RicePrice
and I want to show this database in a combobox. I used code below:

cmbRice.DisplayMember = "fld_RiceType";
cmbRice.ValueMember = "fld_RiceID";
cmbRice.DataSource = rnlq.tbl_Rices.OrderBy(c => c.fld_RiceType).Select(c => c);


I want to change
cmb.DisplayMember
to show
fld_RiceType + fld_RicePrice
. How Can I do this.
fld_RiceType
is
nvarchar(50)
and
fld_RicePrice
is
varchar(50)
.

Answer

I am not sure if this would work but you could try something like this

 OrderBy(c => c.RiceType).Select(c => new { RiceId = c.RiceId, 
                                             RiceType = c.RiceType,
                                             RicePrice = c.RicePrice,
                                             DisplayValue = c.RiceType + ": " + c.RicePrice 
                                           })

And then ofcourse use the DisplayValue as the DisplayMember