datagrid嵌套的文章很多了。
我这里参考的是以下两篇
http://www.cnblogs.com/lovecherry/archive/2005/04/26/145633.htmlhttp://www.microsoft.com/china/MSDN/library/data/dataAccess/NestGridHierData.mspx我只是做了datagrid的嵌套,并没有动态添加模板列那么复杂。
最后的代码给大家。有一部分是vs生成的,懒的写。

<%
@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id=DataGrid1 runat="server" AutoGenerateColumns="False" DataSource="<%# dataSet1 %>">
<Columns>
<asp:BoundColumn DataField="EmployeeID" HeaderText="编号"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="测试列">
<ItemTemplate>
<asp:DataGrid id="DataGrid2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="TerritoryID" HeaderText="编号"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:DataGrid id="DataGrid3" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="TerritoryID" HeaderText="标号"></asp:BoundColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test1


{

/**//// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page

{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.DataSet dataSet1;
protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)

{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)

{
sqlDataAdapter1.Fill(dataSet1);

DataGrid3.DataSource=BindGrid2(1);

DataGrid1.DataBind();
DataGrid3.DataBind();
}
}


Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)

{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet1 = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[]
{

new System.Data.Common.DataTableMapping("Table", "Employees", new System.Data.Common.DataColumnMapping[]
{
new System.Data.Common.DataColumnMapping("EmployeeID", "EmployeeID")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Employees WHERE (EmployeeID = @Original_EmployeeID)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_EmployeeID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=李征;packet size=4096;user id=fa;pwd=fa;data source=李征;persist secur" +
"ity info=False;initial catalog=Northwind";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Employees() VALUES (); SELECT EmployeeID FROM Employees WHERE (Employ" +
"eeID = @@IDENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT EmployeeID FROM Employees";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE Employees SET WHERE (EmployeeID = @Original_EmployeeID); SELECT EmployeeID" +
" FROM Employees WHERE (EmployeeID = @EmployeeID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_EmployeeID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EmployeeID", System.Data.SqlDbType.Int, 4, "EmployeeID"));
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();

}
#endregion

private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{
}

private DataView BindGrid2(int EmployeeID)

{
SqlConnection myconn=new SqlConnection();
myconn.ConnectionString="workstation id=李征;packet size=4096;user id=fa;pwd=fa;data source=李征;persist secur" +
"ity info=False;initial catalog=Northwind";

SqlDataAdapter myda=new SqlDataAdapter("select TerritoryID from EmployeeTerritories where EmployeeID="+EmployeeID.ToString(),myconn);

DataSet ds=new DataSet();

myda.Fill(ds);

return ds.Tables[0].DefaultView;
}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{
DataGrid dgt = (DataGrid)e.Item.FindControl("DataGrid2");

dgt.DataSource=BindGrid2(int.Parse(e.Item.Cells[0].Text.ToString()));
dgt.DataBind();

//Label lb=(Label)e.Item.FindControl("l1");

//lb.Text="111";

}

}
}
}

挑重点看看吧。。。
再说到资源消耗,这么嵌套真有点资源杀手的味道。大部分资源都消耗在对控件的消耗上了。
真难想象再象我引用文章那样的给嵌套的dtatgrid在动态创建列会怎样。
posted on 2005-09-14 09:47
第一控制.NET 阅读(123)
评论(0) 编辑 收藏 所属分类:
3.Net1.1