Dynamic | pagination | template | Data in the last given a dynamic template column An example, users asked me update the operation and pagination how to do, the following gives the code.
Front desk:
<%@ Page language= "C #" codebehind= "WebForm30.aspx.cs" autoeventwireup= "false" inherits= "csdn. WebForm30 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm30</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<link href= "Css.css" rel= "stylesheet" type= "Text/css" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server" ></asp:DataGrid>
</form>
</body>
</HTML>
Background:
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 Csdn
{
<summary>
Summary description of the WEBFORM30.
</summary>
public class WebForm30: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)
{
Bindgrid ();
}
Createdatagrid ()//Make some DataGrid settings
}
protected void Createdatagrid ()
{
datagrid1.autogeneratecolumns=false;//do not enable automatic column generation
datagrid1.cssclass= "Border";//Border style
datagrid1.borderwidth=0;
datagrid1.cellspacing=1;
datagrid1.cellpadding=5;
Datagrid1.itemstyle.cssclass= "item";//Normal column style
Datagrid1.headerstyle.cssclass= "header";//Header style
Datagrid1.pagerstyle.cssclass= "header";//Footer style
Datagrid1.datakeyfield= "Stuid";//Primary key field
datagrid1.allowpaging=true;//Allow paging
datagrid1.pagesize=5;//Paging Size
datagrid1.pagerstyle.mode=pagermode.numericpages;//Digital Form Paging
EditCommandColumn ecc=new EditCommandColumn ();//Update button column
Ecc. buttontype=buttoncolumntype.pushbutton;//Press button
Ecc. edittext= "edit";
Ecc. canceltext= "Cancellation";
Ecc. updatetext= "Update";//Button text
DATAGRID1.COLUMNS.ADD (ECC);//Add button Column
Datagrid1.editcommand+=new Datagridcommandeventhandler (Datagrid1_editcommand);
Datagrid1.updatecommand+=new Datagridcommandeventhandler (Datagrid1_updatecommand);
Datagrid1.cancelcommand+=new Datagridcommandeventhandler (Datagrid1_cancelcommand);//Update, Cancel, edit event Registration
Datagrid1.pageindexchanged + = new System.Web.UI.WebControls.DataGridPageChangedEventHandler (datagrid1_ pageindexchanged)//paging event registration, where you need to note the location of the registration event code, cannot be placed in the Bindgrid ()
Setbind (); Binding Data
}
protected void Bindgrid ()
{
TemplateColumn tm=new TemplateColumn ();
Tm. Itemtemplate=new ColumnTemplate1 ()//Normal column
Tm. Edititemtemplate=new ColumnTemplate2 ()//Edit column
Tm. headertext= "Name";
DataGrid1.Columns.AddAt (0,TM);//Add the first template column to the first column
TemplateColumn tm2=new TemplateColumn ();
TM2. Itemtemplate=new ColumnTemplate3 ();
TM2. Edititemtemplate=new ColumnTemplate4 ();
TM2. headertext= "College";
DataGrid1.Columns.AddAt (1,TM2);//Add the first template column in the second column
Datagrid1.itemdatabound + = new System.Web.UI.WebControls.DataGridItemEventHandler (datagrid1_itemdatabound);// Data binding event Registration, where you need to be aware of registering event codes
Setbind ();
}
protected void Setbind ()
{
SqlConnection conn=new SqlConnection (system.configuration.configurationsettings.appsettings["Conn"));
SqlDataAdapter Da=new SqlDataAdapter ("select * from STU,DEP where stu.studepid=dep.depid", conn);
DataSet ds=new DataSet ();
Da. Fill (ds, "Table1");
This. Datagrid1.datasource=ds. tables["Table1"];
This. Datagrid1.databind ();
}
private void Datagrid1_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
SqlConnection conn=new Sqlconne