使用c#将数据绑定到下拉列表 [英] bind data to dropdownlist with c#

查看:123
本文介绍了使用c#将数据绑定到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道

如何将数据库中的数据绑定到带有C#代码的下拉列表中

i想要从表中选择productID和ProductName(产品

并绑定到下拉列表

显示项目中的ProductName

和Datekeyfield的prodictId

我知道应该使用sqldatasource

i want know
how i can bind a data from database into a dropdownlist with C# code
i want select productID and ProductName from table(product
and bind to dropdownlist
show ProductName in items
and prodictId for Datekeyfield
only i know should use a sqldatasource

推荐答案

cmd.CommandText = " Select * From GetUsersID";
        cmd.Connection = conn;
        conn.Open();
        DataTable dt  = new DataTable();

        dt.Load(cmd.ExecuteReader());
        conn.Close();

        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind(); 





或转到链接: -

http://www.dotnetspider.com/resources/7644-Bind-DropDownList-from-DataBase-Using-c.aspx [ ^ ]


dropdownlistid.DataSource = ds.Tables[0];
dropdownlistid.DisplayMember = "Your Display Column Name from Data Table";
dropdownlistid.ValueMember= "Your Value Column Name from DataTable";
dropdownlistid.DataBind();



或转到此站点点击这里


您好,

您可以使用这些绑定下拉列表方式。

1。使用ListItemCollection。

2。使用Datatable。

3。使用词典。





在你的Aspx中



Hi,
You can bind dropdownlist by using these ways.
1. By using ListItemCollection.
2. By using Datatable.
3. By using Dictionary.


In your Aspx

<div>
 <asp:dropdownlist id="drpDownNames" runat="server" xmlns:asp="#unknown">
 </asp:dropdownlist>
</div>





在您的Aspx代码后面的文件





In Your Aspx code behind file

ListItemCollection collection = new ListItemCollection();
collection.Add(new ListItem("Suits"));
collection.Add(new ListItem("Harvey Spector"));
collection.Add(new ListItem("Jessica Pearson"));
collection.Add(new ListItem("Mike Ross"));
collection.Add(new ListItem("Donna Paulson"));
collection.Add(new ListItem("Rachel"));
collection.Add(new ListItem("Travis Tanner"));

//Pass ListItemCollection as datasource
drpDownNames.DataSource = collection;
drpDownNames.DataBind();





2。使用数据表





2. By Using Datatable

//Create DataTable this can be from database also.
DataTable dtName = new DataTable();

//Add Columns to Table
dtName.Columns.Add(new DataColumn("DisplayMember"));
dtName.Columns.Add(new DataColumn("ValueMember"));

//Now Add Values
dtName.Rows.Add("Suits","0");
dtName.Rows.Add("Harvey Spector","1");
dtName.Rows.Add("Jessica Pearson","2");
dtName.Rows.Add("Mike Ross","3");
dtName.Rows.Add("Donna Paulson","4");
dtName.Rows.Add("Rachel","5");

//At Last Bind datatable to dropdown.
drpDownNames.DataSource  = dtName;
drpDownNames.DataTextField = dtName.Columns["DisplayMember"].ToString();
drpDownNames.DataValueField = dtName.Columns["ValueMember"].ToString();
drpDownNames.DataBind();





3。使用字典





3. By Using Dictionary

Dictionary<string,> namesCollection = new Dictionary<string,>();

namesCollection.Add("Suits", "0");
namesCollection.Add("Harvey Spector", "1");
namesCollection.Add("Jessica Pearson", "2");
namesCollection.Add("Mike Ross", "3");
namesCollection.Add("Donna Paulson", "4");
namesCollection.Add("Rachel", "5");

drpDownNames.DataSource = namesCollection;
drpDownNames.DataTextField = "Key";
drpDownNames.DataValueField = "Value";
drpDownNames.DataBind();





我们可以看到我们是否使用数据表或字典,显然我们需要提到datavaluefield和显示成员,但在ListItemCollection的情况下,我们不需要设置这两个属性。

最终,这是你的选择,让你更容易理解和易于实现。



希望这会有所帮助。



As we can see if we use datatable or Dictionary, explicitly we need to mention datavaluefield and display member, but in case of ListItemCollection we do not need to set these two properties.
Ultimately, this is your choice which ever makes you more understandable and easy to implement.

Hope this helps.


这篇关于使用c#将数据绑定到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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