All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
WML Script语法基础_[XML教程]
ASP.NET编程入门随想之信客_[Asp.Net教程]
小议优化ASP.NET应用性能之ViewState篇_[Asp.Net教程]
PHP下一代的五个framework介绍_php资料_编程技术
SQL循序渐进(8)删除记录_[SQL,Server教程]
asp.net连接Access数据库_[Asp.Net教程]
SQL Server 数据库文件存放在何处_[SQL Server教程]
配置文件中PHP最常用四个ini函数_[PHP教程]
SQL Server视图使用中4个限制条件_[SQL Server教程]
asp.net,后台代码如何遍历checkbox_[Asp.Net教程]
Reading number is top 10 pictures
遇到插队的怎么办?
Ashlynn Brooke photograph of a group2
9.3阅兵全景图3-外国方阵梯队和坦克方阵梯队
Lewd,it is too lewd.
The world's top ten most beautiful railway station2
运动的范冰冰1
007 James. bond's new lover
Men's and women's orgasms
Azusa Yamamoto2
日本小萝莉2
Download software ranking
双旗镇刀客A
WebService在.NET中的实战应用教学视频 → 第5集
Take off clothes to survival
超级战舰
都市狐狸姑娘传
SP3 for SQL2000
Tram sex maniac 2 (H) rar bag19
Professional killers2 data package
asp.net技术内幕
徐若瑄成名作“魔鬼天使”
delv published in(发表于) 2014/1/6 8:47:55 Edit(编辑)
asp.net,2.0中gridview里嵌套dropdownlist_[Asp.Net教程]

asp.net,2.0中gridview里嵌套dropdownlist_[Asp.Net教程]

asp.net 2.0中gridview里嵌套dropdownlist_[Asp.Net教程]

在asp.net 2.0中,在一个gridview里,可以嵌套进一个dropdownlist,这是十分容易的事情,而这里讲的是,
在每个dropdownlist里,都绑定的是不同的内容,比如在northwind数据库中,可以用GRIDVIEW显示出
每个category类别,同时每一行的category类别里可以已dropdonwlist下拉框的形式,列出该分类下的所有
产品.下面介绍实现的方法


首先是页面部分的代码












在codebehind部分,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{


// This is because Table[1] contains Categories


GridView1.DataSource = GetDataSet().Tables[1];


GridView1.DataBind();


}



}
private DataSet GetDataSet()
{


string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p


SELECT c.CategoryID,c.CategoryName FROM Categories c";


string connectionString = "Server=localhost;Database=Northwind;user id=sa;password=123456";


SqlConnection myConnection = new SqlConnection(connectionString);


SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);


DataSet ds = new DataSet();


ad.Fill(ds);


return ds;


}
在上面的代码中,首先我们通过典型的dataset返回,绑定到gridview上去,注意这里sql语句里有两句,第一句是返回产品,第二句是返回所有的类别category,而在绑定gridview时,我们用
GridView1.DataSource = GetDataSet().Tables[1];,将第一个table表里的category记录绑定到gridview里去,接下来,我们要处理模版列中的dropdownlist了,这个可以在row_databound事件中写入代码,如下
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable myTable = new DataTable();


DataColumn productIDColumn = new DataColumn("ProductID");


DataColumn productNameColumn = new DataColumn("ProductName");


myTable.Columns.Add(productIDColumn);


myTable.Columns.Add(productNameColumn);


DataSet ds = new DataSet();


ds = GetDataSet();


int categoryID = 0;


string expression = String.Empty;


if (e.Row.RowType == DataControlRowType.DataRow)
{


categoryID = Int32.Parse(e.Row.Cells[0].Text);


expression = "CategoryID = " + categoryID;


DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");


DataRow[] rows = ds.Tables[0].Select(expression);


foreach (DataRow row in rows)
{


DataRow newRow = myTable.NewRow();


newRow["ProductID"] = row["ProductID"];


newRow["ProductName"] = row["ProductName"];


myTable.Rows.Add(newRow);


}


ddl.DataSource = myTable;


ddl.DataTextField = "ProductName";


ddl.DataValueField = "ProductID";


ddl.DataBind();


}


}
这里的原理大致如下:
首先,我们建立了一个datatable,包含了productid,productname列,然后将其与 gridview绑定,之后再用
categoryID = Int32.Parse(e.Row.Cells[0].Text);


expression = "CategoryID = " + categoryID;
构造一个筛选表达式,这里指定为categoryID,然后通过
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
,找出符合其类别等于某一categoryID的所有产品,这里构造出datarow集合了,最后,使用循环
,将某类别下的产品全部添加到datatable中去,最后将datatable和dropdownlist绑定,就实现功能了







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.