GridView动态添加列的两篇文章

转自:http://blog.csdn.net/liang4571231/archive/2009/03/26/4025944.aspx

 

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);

                break;

            case DataControlRowType.DataRow: //模版列内容——加载hyperlink1

                HyperLink hl = new HyperLink();

                hl.ID = "hyperlink1";

                hl.NavigateUrl = "erro.aspx";

                hl.Text = "asddddd";

                ctlContainer.Controls.Add(hl);

                break;

        }

    }

 

}

        GridView gdview = new GridView();// (GridView)m_map.Page.FindControl("GridView1");

 

 

        gdview.AutoGenerateColumns = false;

        gdview.DataKeyNames = new string[] { "SDE.ST_ZD.ZD_DJH" };

 

        BoundField bf1 = new BoundField();

        BoundField bf2 = new BoundField();

        TemplateField tf = new TemplateField();

 

        bf1.HeaderText = "地籍号";

        bf1.DataField = "SDE.ST_ZD.ZD_DJH";

        bf1.ReadOnly = true;

 

        bf2.HeaderText = "权利人名称";

        bf2.DataField = "SDE.ST_ZD.QLR_MC";

 

        tf.HeaderText = "模板列测试";

        tf.ItemTemplate = new MyTemplate("aaaa", DataControlRowType.DataRow);

 

        CommandField cf = new CommandField();

        cf.ButtonType = ButtonType.Button;

        cf.ShowCancelButton = true;

        cf.ShowEditButton = true;

 

        gdview.Columns.Add(bf1);

        gdview.Columns.Add(bf2);

        gdview.Columns.Add(tf);

 

 

 

文章二:http://blog.csdn.net/Sandy945/archive/2009/07/31/4397142.aspx

 

GridView动态添加模板列 这个case 经常会遇到,网上相关资料也不少,

但是关于如何动态为添加在模板列中的控件添加事件却没有。本文就是为解决这种情况而写。

代码如下:

GridViewTemplateDemo.aspx


view plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTemplateDemo.aspx.cs" Inherits="GridView_GridViewTemplateDemo" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
   
     <title>GridView动态添加模板列的例子</title> 
   
 </head> 
   
 <body> 
   
     <form id="form1" runat="server"> 
   
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
   
       </asp:GridView> 
   
     </form> 
   
 </body> 
   
 </html>  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTemplateDemo.aspx.cs" Inherits="GridView_GridViewTemplateDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
 
     <title>GridView动态添加模板列的例子</title>
 
 </head>
 
 <body>
 
     <form id="form1" runat="server">
 
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
 
       </asp:GridView>
 
     </form>
 
 </body>
 
 </html> 

GridViewTemplateDemo.aspx.cs


view plaincopy to clipboardprint?
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
public partial class GridView_GridViewTemplateDemo : System.Web.UI.Page  
{  
    private DataTable getDateTable()  
    {  
        DataTable dt = new DataTable();  
        dt.Columns.Add(new DataColumn("ID", typeof(Int32)));  
        dt.Columns.Add(new DataColumn("Name", typeof(String)));  
        DataRow dr = dt.NewRow();  
        dr[0] = 1;  
        dr[1] = "01";  
        dt.Rows.Add(dr);  
        dr = dt.NewRow();  
        dr[0] = 2;  
        dr[1] = "01011";  
        dt.Rows.Add(dr);  
        dr = dt.NewRow();  
        dr[0] = 3;  
        dr[1] = "0120";  
        dt.Rows.Add(dr);  
        dr = dt.NewRow();  
        dr[0] = 4;  
        dr[1] = "01001";  
        dt.Rows.Add(dr);  
        dr = dt.NewRow();  
        dr[0] = 5;  
        dr[1] = "0105";  
        dt.Rows.Add(dr);  
        return dt;  
    }  
 
    protected override void OnInit(EventArgs e)  
    {  
        TemplateField customField = new TemplateField();  
 
        customField.ShowHeader = true;  
        customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");         
        GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "Name");  
        gvt.eh += new GridViewTemplate.EventHandler(lbtn_Click);  
        customField.ItemTemplate = gvt;  
        GridView1.Columns.Add(customField);  
        base.OnInit(e);  
    }  
   
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!IsPostBack)  
        {  
            GridView1.DataSource = getDateTable();  
            GridView1.DataBind();  
        }  
    }  
   
    public void lbtn_Click(object sender, EventArgs e)  
    {  
        ClientScript.RegisterStartupScript(GetType(), "test", "alert('ok');", true);  
    }  
}  
   
     public class GridViewTemplate : ITemplate  
   
     {  
         public delegate void EventHandler(object sender, EventArgs e);  
         public event EventHandler eh;  
   
         private DataControlRowType templateType;  
   
         private string columnName;  
         private string controlID;  
   
    
   
         public GridViewTemplate(DataControlRowType type, string colname)  
   
         {  
   
             templateType = type;  
   
             columnName = colname;  
   
         }  
         public GridViewTemplate(DataControlRowType type,string controlID, string colname)  
         {  
             templateType = type;  
             this.controlID = controlID;  
             columnName = colname;  
         }  
    
   
    public void InstantiateIn(System.Web.UI.Control container)   
    {  
        switch (templateType)  
        {  
            case DataControlRowType.Header:  
                Literal lc = new Literal();  
                lc.Text = columnName;  
                container.Controls.Add(lc);  
                break;  
            case DataControlRowType.DataRow:              
                LinkButton lbtn = new LinkButton();  
                lbtn.ID = this.controlID;  
                if (eh != null)  
                {  
                    lbtn.Click += new System.EventHandler(eh);  
                }  
                lbtn.DataBinding+=new System.EventHandler(lbtn_DataBinding);  
                  
                container.Controls.Add(lbtn);  
                  
                break;  
            default:  
                break;  
        }  
    }  
     void lbtn_DataBinding(object sender, EventArgs e)  
     {  
         LinkButton lbtn = sender as LinkButton;  
         if (lbtn != null)  
         {  
             GridViewRow container = lbtn.NamingContainer as GridViewRow;  
             if (container != null)  
             {  
                 object dataValue = DataBinder.Eval(container.DataItem, columnName);  
                 if (dataValue != DBNull.Value)  
                 {  
                     lbtn.Text = dataValue.ToString();  
                 }  
             }  
         }  
     }  
 
   } 

你可能感兴趣的:(GridView)