Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SqlCommand cmd = new SqlCommand("select * from user_login where groupid=1", cnn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlstudentname.DataSource=ds;
            ddlstudentname.DataTextField="name";
            ddlstudentname.DataVlaueField="name";
            ddlstudentname.DataBind();


I am fill dropdown using this way ,but i have to add one more thing that is not in dataset of my query.
so what is syntex of it ?
Posted
Comments
marcio_krul 11-Jan-13 8:49am    
the sintax is:

youdropdown.insert(position,youItem);

Exemple:

dropdown1.Insert(0,new ListItem("value","Text")); insert data in a first position;
dropdown1.Add(new ListItem("value","Text")); insert into a last position;

Hi,

C#
ds.Tables[0].Rows.Add(new object[] { "Your Element" });


If you want it as first then

C#
ds.Tables[0].Rows.InsertAt(new DataRow() { ItemArray=new object[] { "Your Element" }},0);
 
Share this answer
 
Comments
Member 9511889 11-Jan-13 8:50am    
this element ( for Ex:'Please select' i have to put it as first) is going in dropdown or nor. If going then i have to not consider it as data (for Ex: if i select 'please select' from dropdown then it will ask for select any data from dropdown to user.)
Suvabrata Roy 11-Jan-13 8:53am    
ds.Tables[0].Rows.InsertAt(new DataRow() { ItemArray=new object[] { "Please Select" }},0);
after databinding, add the following:

ddlstudentname.Items.Insert(0, new ListItem("Select","NA"));
 
Share this answer
 
Comments
Member 9511889 11-Jan-13 9:01am    
thanks for your ans.i have one question.
here what is meaning of "NA".how it affect?
Om Prakash Pant 11-Jan-13 9:04am    
As an example, i have added some value. "Select" is text and "NA" is value in above example.
After the DataBind()you can do one of the following:

C
ddlstudentname.Items.Add("Foo"); //Adds to the end of the list, Text set to Foo, no value
ddlstudentname.Items.Add(new ListItem("Foo")); //Adds to the end of the list, Text set to Foo, no value ( same as above)
ddlstudentname.Items.Add(new ListItem("Foo","Bar")); //Adds to the end of the list, Text set to Foo, Value set to Bar

ddlstudentname.Items.Insert(0, "Foo"); //Adds to the start of the list (index 0), Text set to Foo, no value
ddlstudentname.Items.Insert(0, new ListItem("Foo")); //Adds to the start of the list (index 0), Text set to Foo, no value ( same as above)
ddlstudentname.Items.Insert(0, new ListItem("Foo","Bar")); //Adds to the start of the list (index 0), Text set to Foo, Value set to Bar
 
Share this answer
 
youdropdown.insert(position,youItem);

Exemple:

dropdown1.Insert(0,new ListItem("value","Text")); insert data in a first position; dropdown1.Add(new ListItem("value","Text")); insert into a last position;
 
Share this answer
 

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