例子比较简单,直接贴代码了,例子达到以下功能:
(1)两个DataGrid嵌套
(2)外面一个DataGrid分页
(3)里面一个DataGrid动态增加模板列 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列


(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
<HTML>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
<HEAD>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<title>Test</title>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<meta content="C#" name="CODE_LANGUAGE">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<meta content="JavaScript" name="vs_defaultClientScript">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
</HEAD>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
<body>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
<form id="Form1" method="post" runat="server">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="2" AllowPaging="True">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                
<Columns>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                    
<asp:TemplateColumn HeaderText="分类名">
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                        
<ItemTemplate>

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                            
<asp:DataGrid id="DataGrid2" runat="server" AutoGenerateColumns="false"></asp:DataGrid>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                        
</ItemTemplate>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                    
</asp:TemplateColumn>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                
</Columns>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
</asp:datagrid></form>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
</body>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
</HTML>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列


 

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列using System;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Collections;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.ComponentModel;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Data;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Data.SqlClient;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Drawing;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Web;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Web.SessionState;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Web.UI;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Web.UI.WebControls;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
using System.Web.UI.HtmlControls;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
namespace csdn2
{

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
/// WebForm45 的摘要说明。
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
/// </summary>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    public class WebForm45 : System.Web.UI.Page
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
private void Page_Load(object sender, System.EventArgs e)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
// 在此处放置用户代码以初始化页面
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
            if(!IsPostBack)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                SetBind();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
private void SetBind()
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            SqlConnection conn
=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            SqlDataAdapter da
=new SqlDataAdapter("select * from class;select * from topic",conn);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            DataSet ds
=new DataSet();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            da.Fill(ds);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            ds.Relations.Add(
"class_topic",ds.Tables[0].Columns["classid"],ds.Tables[1].Columns["topicclassid"]);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.DataGrid1.DataSource=ds.Tables[0];
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.DataGrid1.DataBind();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
override protected void OnInit(EventArgs e)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
//
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
//
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
            InitializeComponent();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
base.OnInit(e);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
/// 此方法的内容。
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
/// </summary>
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        private void InitializeComponent()
{    
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.Load += new System.EventHandler(this.Page_Load);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
#endregion
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                DataGrid dgrd
=(DataGrid)e.Item.FindControl("DataGrid2");
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                dgrd.ItemDataBound 
+= new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid2_ItemDataBound);
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                TemplateColumn tm
=new TemplateColumn(); 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                tm.ItemTemplate
=new ColumnTemplate(); 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                tm.HeaderText
="主题名"
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                dgrd.Columns.Add(tm); 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                dgrd.DataSource
=((DataRowView)e.Item.DataItem).Row.GetChildRows("class_topic");
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                dgrd.DataBind();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            SetBind();
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列                ((Label)e.Item.FindControl(
"topicname")).Text=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"[\"topicname\"]"));
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    }

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    
public class ColumnTemplate : ITemplate 

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        
public void InstantiateIn(Control container)       

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            Label l
=new Label(); 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            l.ID
="topicname";
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列            container.Controls.Add(l); 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列        }
 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列    }
 
(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列}

(原创)嵌套的DataGrid如何为子DataGrid动态增加模板列

相关文章: