Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My stored procedure is like this

SELECT Id, StudentName
FROM xyz

I have a drop down list in asp.net, which I am loading as :

ddlA.DataSource = // Some source
ddlA.DataTextField = "Id" + " -" + "StudentName";
ddlA.DataValueField = "Id";
ddlA.DataBind();
ddlA.Items.Insert(0, new ListItem(" Select one", "0"));

But at the Databind() statement, I am getting this error:

System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Id-StudentName'.

In text part of the dropdown list, I want to display the concatenated value of Id - StudentName.

How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use LINQ to make a new datasource containing a displayfield which is formatted, the way you want it to be, like:

var datasource = from x in products
                 select new {
                     x.Id,
                     x.Code,
                     x.Description,
                     DisplayField = String.Format("{0} ({1})", x.Code, x.Description)
                 };

comboBox.DataSource = datasource;
comboBox.DataValueField = "Id";
comboBox.DataTextField = "DisplayField";
comboBox.DataBind();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...