Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have to bind items from database to the dropdown list with ID as the value and Name as Text. But I want 'select' item in the zeroth positon. I'm using the following code:

C#
SqlDataAdapter da1 = new SqlDataAdapter(strng, con);
DataTable dt = new DataTable();
da1.Fill(dt);
ddLogo.DataSource = dt;
//ddLogo.DataBind();
ddLogo.DataTextField = "Ename";
ddLogo.DataValueField = "ID";
ddLogo.SelectedIndex = -1;
ddLogo.DataBind();
ddLogo.Items.Insert(0, new ListItem("--select--", "--select--"));

But when I'm adding the line
ddLogo.Items.Insert(0, new ListItem("--select--", "--select--"));
only the zeroth position is there in the list.
What I've to do.

Thank you
Posted
Updated 6-Sep-12 3:39am
v2
Comments
[no name] 6-Sep-12 9:40am    
try ddLogo.Items.Insert(0, new ListItem("--select--", "--select--")); ddLogo.DataBind(); instead and see what happens.

You could use AppendDataBoundItems[^] property: set it to true, add your items (you could even do it in markup), then set DataSource and call DataBind()
 
Share this answer
 
Comments
Zukiari 6-Sep-12 23:46pm    
Thank You so much........
Insert into your data table dont go for fresh insert. Do this after fetch from db

C#
SqlDataAdapter da1 = new SqlDataAdapter(strng, con);
DataTable dt = new DataTable();
da1.Fill(dt);

// generate the data you want to insert
DataRow toInsert = dt.NewRow();

// Then add the new row to the collection.
row["EName"] = "--Select--";
row["ID"] = "--Select--";

// insert in the desired place
dt.Rows.InsertAt(toInsert, 0);
ddLogo.DataSource = dt;
ddLogo.DataTextField = "Ename";
ddLogo.DataValueField = "ID";
ddLogo.DataBind();
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900