如何在下拉列表(ASP.NET)中添加表列 [英] How to add table columns in a dropdownlist (ASP.NET)
问题描述
这是到目前为止我提出的一个例子.非常简单
http://img.iz.my/show.php/765_tnya.png.html [ ^ ]
我只是将Northwind数据库绑定到一个gridview中(请注意,该gridview中的数据来自多个表)
所以我想做的是使用dropdownlist和文本框(搜索)进行某种快速的数据过滤.
如何在下拉列表中放入联系人姓名",城市"和国家/地区"列?我不希望下拉列表中的记录像在示例中那样
here''s an example what i''ve came up so far. its pretty simple
http://img.iz.my/show.php/765_tnya.png.html[^]
i just bind the northwind database in a gridview (mind the data in that gridview is from multiple tables)
so what i want to do is some sort of quick data filtering using dropdownlist together with textbox (search)
how can i put the columns "Contact Name", "City" and "Country" in the dropdown? i don''t want the records inside the dropdown like i did in the example
推荐答案
嗨 选中此示例将指导您
Hi Check this Example will Guide you
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
con.Open();
//Change with your select statement .
using (SqlCommand cmd = new SqlCommand("select * from test1", con))
{
DataTable dt = new DataTable();
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
Dictionary<int,> lst = new Dictionary<int,>();
foreach (DataRow row in dt.Rows)
{
//Add values to Dictionary
string val = row[1].ToString() + " , " + row[2].ToString() + " , " + row[3].ToString();
lst.Add(Convert.ToInt32(row[0]), val);
}
DropDownList1.DataSource = lst;
DropDownList1.DataTextField = "Value";
DropDownList1.DataValueField = "Key";
DropDownList1.DataBind();
}
}
}
}
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
最好的问候
米特瓦里(M.Mitwalli)
Best Regards
M.Mitwalli
这篇关于如何在下拉列表(ASP.NET)中添加表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!