如何使用查询在下拉列表中添加新的iteam [英] how to add new iteam in dropdown using query

查看:92
本文介绍了如何使用查询在下拉列表中添加新的iteam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我使用这种方式填充下拉列表,但我还要添加一个不在我的查询的数据集中。

那么什么是它的syntex?

解决方案





 ds.Tables [ 0 ]。Rows.Add( new   object  [] { 您的元素}); 





如果你想要它,那么



 ds.Tables [ 0 ]。Rows.InsertAt( new  DataRow(){ItemArray =  new   object  [] { 您的元素}}, 0 ); 


ing,添加以下内容:



ddlstudentname.Items.Insert(0,new ListItem(Select,NA));


DataBind()之后,您可以执行以下操作之一:



 ddlstudentname.Items.Add(  Foo);  //  添加到列表末尾,文本设置为Foo,没有值 
ddlstudentname .Items.Add( new ListItem( Foo< /跨度>)); // 添加到列表末尾,文本设置为Foo,没有值(与上面相同)
ddlstudentname.Items.Add( new ListItem( Foo Bar)); // 添加到列表末尾,文本设置为Foo,值设置为Bar

ddlstudentname.Items.Insert( 0 ); // 添加到列表的开头(索引0),文本设置为Foo,没有值
ddlstudentname.Items.Insert( 0 new ListItem( Foo)); // 添加到列表的开头(索引0),文本设置为Foo,没有值(与上面相同)
ddlstudentname.Items.Insert( 0 new ListItem( Foo )); // 添加到列表的开头(索引0),文本设置为Foo,值设置为Bar


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 ?

解决方案

Hi,

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



If you want it as first then

ds.Tables[0].Rows.InsertAt(new DataRow() { ItemArray=new object[] { "Your Element" }},0);


after databinding, add the following:

ddlstudentname.Items.Insert(0, new ListItem("Select","NA"));


After the DataBind()you can do one of the following:

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


这篇关于如何使用查询在下拉列表中添加新的iteam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆