using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace Boyuanec.Utility
{
         public class MyTemplate : ITemplate
        {

                 private string strColumnName;

                 private DataControlRowType dcrtColumnType;

                 public MyTemplate( string strColumnName, DataControlRowType dcrtColumnType)
                {

                         this.strColumnName = strColumnName;

                         this.dcrtColumnType = dcrtColumnType;

                }

                 public void InstantiateIn(Control ctlContainer)
                {

                         switch (dcrtColumnType)
                        {

                                 case DataControlRowType.Header: //列标题

                                        Literal ltr = new Literal();

                                        ltr.Text = strColumnName;

                                        ctlContainer.Controls.Add(ltr);
                                        
                                        HtmlInputCheckBox chk = new HtmlInputCheckBox();

                                        chk.ID = "chkError";

                                        chk.Value = "";

                                        chk.Attributes.Add( "onclick", "CheckAll('GVSF6',this)");

                                        ctlContainer.Controls.Add(chk);

                                         break;

                                 case DataControlRowType.DataRow: //模版列内容

                                        CheckBox chbError = new CheckBox();



                                        chbError.ID = "chbError";

                                        chbError.Text = "";

                                        chbError.DataBinding += new EventHandler(cbItem_DataBinding);

                                        ctlContainer.Controls.Add(chbError);

                                         break;
                        }
                }

                 private void cbItem_DataBinding( object sender, EventArgs e)
                {
                        CheckBox cbx = (CheckBox)sender;
                         //取得GridViewRow(包含CheckBox控件)    
                        
                        GridViewRow row = (GridViewRow)cbx.NamingContainer;
                         //进行数据绑定    

                         //cbx.Checked = Convert.ToBoolean(DataBinder.Eval(row.DataItem, "WrongFlag"));
                }

        }
}
调用
ExchangeDate();
                GVSF6.Columns.Clear();
                Hashtable ht = GetTestPoint();
                BoundField[] BoundFields = GetStaticBoundFields();
                BoundField[] BoundFields1 = GetDynamicBoundFields(ht);
                 if (BoundFields1.Length >0)
                {
                         for ( int i = 0; i < BoundFields.Length; i++)
                        {
                                GVSF6.Columns.Add(BoundFields[i]);
                        }
                         for ( int j = 0; j < BoundFields1.Length; j++)
                        {
                                GVSF6.Columns.Add(BoundFields1[j]);
                        }
                        TemplateField templateField = new TemplateField();
                        templateField.HeaderTemplate = new MyTemplate( "全选", DataControlRowType.Header);
                        templateField.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow);
                        GVSF6.Columns.Add(templateField);
                         try
                        {
                                DataSet ds = new BLLSF6().GetList(ddlCompany.SelectedValue, ddlPlant.SelectedValue, ddlLogicEquipment.SelectedValue, txtDateStart.Value, txtDateEnd.Value, Convert.ToInt32(ddlDataType.SelectedValue), GVSF6.PageSize, AspNetPager1.CurrentPageIndex, out recordCount);
                                 this.GVSF6.DataSource = ds.Tables[0];
                                 this.GVSF6.DataBind();
                        }
                         catch
                        { }
                }
                 else
                {
                        ScriptManager.RegisterStartupScript(UpdatePanel3, GetType(), "msg", "alert('请选择测量项')", true);
                }