How a nested DataGrid dynamically adds a template column to a child datagrid

Source: Internet
Author: User
Tags eval visual studio
Datagrid| News | templates

The example is simpler, the direct code is pasted, the example achieves the following function:
(1) Two DataGrid nesting
(2) outside a DataGrid paging
(3) inside a DataGrid dynamically adding template columns

<%@ 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= "category name" >
<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>
Summary description of the 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)
{
Place user code here to initialize page
if (! IsPostBack)
{
Setbind ();
}
}

private void Setbind ()
{
SqlConnection conn=new SqlConnection (system.configuration.configurationsettings.appsettings["Conn"));
SqlDataAdapter Da=new SqlDataAdapter ("select * FROM Class;select * to 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 Forms Designer generated Code #region Web Forms Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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= "topic name";
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);
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.