使用c#从下拉列表中根据所选值填充列表视图 [英] populate listview based on selected value from a dropdownlist using c#
本文介绍了使用c#从下拉列表中根据所选值填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
LISTVIEW1:基于类别ID我从下拉列表中收到我需要的更新我的列表视图
我的代码:
protected void DropDownList1_SelectedIndexChanged( object sender,EventArgs e)
{
DataTable table = new DataTable();
// int id = Convert.ToInt32(DropDownList1.SelectedValue);
// int id = int.Parse(DropDownList1.SelectedValue.ToString());
string cat_id = DropDownList1.SelectedValue.ToString();
int id;
if (!( int .TryParse(cat_id.ToString(), out id)))
return ;
使用(SqlConnection con = new SqlConnection(constr))
{
string sql = 选择ID,名称,描述,category,image,price,categoryid from products WHERE categoryid = @ cid ORDER BY categoryid;
使用(SqlCommand cmd = new SqlCommand(sql,con))
{
使用(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue( @ cid,cat_id);
da.Fill(table);
}
}
}
listview.DataSource = table;
listview.DataBind();
}
}
解决方案
此链接可能对您有用
http:/ /stackoverflow.com/questions/14415189/get-value-of-listview-item-on-selectedindexchanged-on-dropdownlist-in-listview [ ^ ]
http://codeblog.shawson.co.uk/quick-ref/listview-using-a-dropdownlist-populated-using-code-behind/ [ ^ ]
我是sa id较早,如果你得到了你定义的数据表的正确值,那么使用这个函数用数据表填充List视图。
private void LoadList()
{
// 清除ListView控件
listView1.Items.Clear();
int ColCount = dtable.Columns.Count;
// 添加列
for ( int k = 0 ; k < ColCount; k ++)
{
listView1.Columns.Add(dtable.Columns [k] .ColumnName);
}
// 在ListView控件中显示项目
< span class =code-keyword> for ( int i = 0 ; i < span class =code-keyword>< dtable.Rows.Count; i ++)
{
DataRow drow = dtable.Rows [i];
// 只有尚未删除的行
< span class =code-keyword> if (drow.RowState!= DataRowState.Deleted)
{
// < span class =code-comment>定义列表项
ListViewItem lvi = new ListViewItem(drow [ 0 跨度>]的ToString());
for ( int j = 1 ; j < ColCount; j ++)
{
lvi.SubItems.Add(drow [j] .ToString());
}
// 将列表项添加到ListView
listView1.Items.Add(LVI);
}
}
}
hey,im trying to populate my listview based on value selected in dropdownlist. actualy im not getting any errors, but listview is not displayed when user selects any dropdownlist value. DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
LISTVIEW1: based on category id i recieve from dropdownlist i need to update my listview
my code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
//int id = Convert.ToInt32(DropDownList1.SelectedValue);
// int id = int.Parse(DropDownList1.SelectedValue.ToString());
string cat_id = DropDownList1.SelectedValue.ToString();
int id;
if (!(int.TryParse(cat_id.ToString(), out id)))
return;
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select id,name,description,category,image,price,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@cid", cat_id);
da.Fill(table);
}
}
}
listview.DataSource = table;
listview.DataBind();
}
}
解决方案
this link may be useful for you
http://stackoverflow.com/questions/14415189/get-value-of-listview-item-on-selectedindexchanged-on-dropdownlist-in-listview[^]
http://codeblog.shawson.co.uk/quick-ref/listview-using-a-dropdownlist-populated-using-code-behind/[^]
As I said earlier, if you get correct value to the datatable you have defined, then use this function to populate List view using the data table.
private void LoadList() { // Clear the ListView control listView1.Items.Clear(); int ColCount = dtable.Columns.Count; //Add columns for (int k = 0; k < ColCount; k++) { listView1.Columns.Add(dtable.Columns[k].ColumnName); } // Display items in the ListView control for (int i = 0; i < dtable.Rows.Count; i++) { DataRow drow = dtable.Rows[i]; // Only row that have not been deleted if (drow.RowState != DataRowState.Deleted) { // Define the list items ListViewItem lvi = new ListViewItem(drow[0].ToString()); for (int j = 1; j < ColCount; j++) { lvi.SubItems.Add(drow[j].ToString()); } // Add the list items to the ListView listView1.Items.Add(lvi); } } }
这篇关于使用c#从下拉列表中根据所选值填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文