例子比较简单,直接贴代码了,例子达到以下功能:
(1)两个DataGrid嵌套
(2)外面一个DataGrid分页
(3)里面一个DataGrid动态增加模板列
(1)两个DataGrid嵌套
(2)外面一个DataGrid分页
(3)里面一个DataGrid动态增加模板列
<%
@ Page language="c#" Codebehind="WebForm45.aspx.cs" AutoEventWireup="false" Inherits="csdn2.WebForm45"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > Test </ 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" PageSize ="2" AllowPaging ="True" >
< Columns >
< asp:TemplateColumn HeaderText ="分类名" >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem, "classname") %>
< asp:DataGrid id ="DataGrid2" runat ="server" AutoGenerateColumns ="false" ></ asp:DataGrid >
</ ItemTemplate >
</ asp:TemplateColumn >
</ Columns >
</ asp:datagrid ></ form >
</ body >
</ HTML >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > Test </ 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" PageSize ="2" AllowPaging ="True" >
< Columns >
< asp:TemplateColumn HeaderText ="分类名" >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem, "classname") %>
< asp:DataGrid id ="DataGrid2" runat ="server" AutoGenerateColumns ="false" ></ asp:DataGrid >
</ ItemTemplate >
</ asp:TemplateColumn >
</ 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 csdn2
{
/**//// <summary>
/// WebForm45 的摘要说明。
/// </summary>
public class WebForm45 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
}
}
private void SetBind()
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from class;select * from topic",conn);
DataSet ds=new DataSet();
da.Fill(ds);
ds.Relations.Add("class_topic",ds.Tables[0].Columns["classid"],ds.Tables[1].Columns["topicclassid"]);
this.DataGrid1.DataSource=ds.Tables[0];
this.DataGrid1.DataBind();
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGrid dgrd=(DataGrid)e.Item.FindControl("DataGrid2");
dgrd.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid2_ItemDataBound);
TemplateColumn tm=new TemplateColumn();
tm.ItemTemplate=new ColumnTemplate();
tm.HeaderText="主题名";
dgrd.Columns.Add(tm);
dgrd.DataSource=((DataRowView)e.Item.DataItem).Row.GetChildRows("class_topic");
dgrd.DataBind();
}
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
SetBind();
}
private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("topicname")).Text=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"[/"topicname/"]"));
}
}
}
public class ColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Label l=new Label();
l.ID="topicname";
container.Controls.Add(l);
}
}
}
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 csdn2
{
/**//// <summary>
/// WebForm45 的摘要说明。
/// </summary>
public class WebForm45 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
}
}
private void SetBind()
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from class;select * from topic",conn);
DataSet ds=new DataSet();
da.Fill(ds);
ds.Relations.Add("class_topic",ds.Tables[0].Columns["classid"],ds.Tables[1].Columns["topicclassid"]);
this.DataGrid1.DataSource=ds.Tables[0];
this.DataGrid1.DataBind();
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGrid dgrd=(DataGrid)e.Item.FindControl("DataGrid2");
dgrd.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid2_ItemDataBound);
TemplateColumn tm=new TemplateColumn();
tm.ItemTemplate=new ColumnTemplate();
tm.HeaderText="主题名";
dgrd.Columns.Add(tm);
dgrd.DataSource=((DataRowView)e.Item.DataItem).Row.GetChildRows("class_topic");
dgrd.DataBind();
}
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
SetBind();
}
private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("topicname")).Text=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"[/"topicname/"]"));
}
}
}
public class ColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Label l=new Label();
l.ID="topicname";
container.Controls.Add(l);
}
}
}