Format DropDownList.TextValue

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();

Method 2

DropDownList1.DataTextFormatString = "{0} - {1}";
DropDownList1.DataTextField = "Id,StudentName";

It seems that it’s not achievable automatically, e.g. see this MS Connect ticket.


Thus do that programmatically:

foreach (var row in table.Rows)
{
    row.Field<string>("text") = String.Format(..);
}

or

foreach (var item in data)
{
    new ListItem { Text = String.Format(..); }; 
}

Method 3

You can achieve it by following code

dsStudent.Tables[0].Columns.Add("IdWithStudenName",typeof(string),"Id + ' - ' + StudenName");
DropDownList1.DataSource = dsStudent;
DropDownList1.DataTextField = "IdWithStudenName";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Please select"));

For more understanding refer here.

Method 4

If the DataSource is a DataTable, you can add an “expression” (calculated column) to the table. The expression for the new column would look like this:

newcolumn.Expression = "Id + ' - ' + StudentName";

Method 5

You can change your stored procedure to:

SELECT Id, Id + " - " StudentName as Text FROM xyz

And change the binding to:

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

Method 6

If you have list of class to fill drop down list, this is probably best to do;

List<FOO> foo = new List<FOO>();

ddlFoo.DataSource = foo.Select(x => 
    new ListItem { Text = x.Text1 + " - " + x.Text2, Value = x.Id.ToString() });
ddlFoo.DataBind();

Method 7

it’s very simple, only you have concat fields in the query

SELECT Id ,Id || ' ' || StudentName ""TEXT"", 
FROM xyz

then you build a drop down list

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x