user3319756 user3319756 - 3 months ago 6
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 fields:

fld_RiceType
,
fld_RiceId
and
fld_RicePrice
.
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)

  • 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

Comments