GridView 动态生成模版列

GridView动态列生成
本文介绍了一种使用模板列动态生成GridView列的方法,并提供了具体的.NET实现代码。通过自定义类实现ITemplate接口,可以根据不同的行类型生成列头和数据行。

最近项目中有用到GridView 的列是动态生成的,并且是用模版列.查找了网上的方法.作法如下:

1. 写一个类.主要继承ITemplate

 

ContractedBlock.gifExpandedBlockStart.gifCode
public class GridViewTemplate : ITemplate   
     {   
         
private DataControlRowType templateType;     //区分列的类型
         private string columnName;                   //名称
       
         
public GridViewTemplate(DataControlRowType type, string colname)   //构造
         {   
             templateType 
= type;   
             columnName 
= colname;   
         }   
       
         
public void InstantiateIn(System.Web.UI.Control container)   
         {   
             
switch (templateType)   
             {   
                 
case DataControlRowType.Header:       //列头           
                     Literal lc = new Literal();          
                     lc.Text 
= "<B>" + columnName + "</B>";   //列头名称        
                     container.Controls.Add(lc);          
                     
break;          
                 
case DataControlRowType.DataRow:      //数据行
                     LinkButton lbnFileName = new LinkButton();   //添加LinkButton控件,正常显示
                     lbnFileName.ID = "lbn" + columnName;
                     lbnFileName.DataBinding 
+= new EventHandler(lbnFileName_DataBinding);//数据绑定
                     lbnFileName.Click += new EventHandler(lbnFileName_Click);//Click 事件
                     container.Controls.Add(lbnFileName);
                     
//Guid
                     Label lblGuid = new Label();    //添加lable ,隐藏
                     lblGuid.ID = "lblGuid" + columnName;
                     lblGuid.DataBinding 
+= new EventHandler(lblGuid_DataBinding);
                     lblGuid.Visible 
= false;
                     container.Controls.Add(lblGuid);
                     
//Type
                     Label lblType = new Label(); //添加lable ,隐藏
                     lblType.ID = "lblType" + columnName;
                     lblType.DataBinding 
+= new EventHandler(lblType_DataBinding);
                     lblType.Visible 
= false;
                     container.Controls.Add(lblType);
                     
break;                         
                 
default:   
         
                     
break;   
             }   
         }

        
void lblType_DataBinding(object sender, EventArgs e)  //Lable 的数据绑定
        {
            Label lbl 
= (Label)sender;
            GridViewRow row 
= (GridViewRow)lbl.NamingContainer;
            lbl.Text 
=  DataBinder.Eval(row.DataItem, "lblType" +columnName).ToString();  
        }

        
void lblGuid_DataBinding(object sender, EventArgs e) //Lable 的数据绑定

        {
             Label lbl 
= (Label)sender;
             GridViewRow row 
= (GridViewRow)lbl.NamingContainer;
             lbl.Text 
=  DataBinder.Eval(row.DataItem, "lblGuid" +columnName).ToString();  

        }

        
void lbnFileName_Click(object sender, EventArgs e) //LinkButton的Click 事件,这里主要是来下载文档
        {
            
try
            {
                GridViewRow objGridViewRow 
= (GridViewRow)((LinkButton)sender).NamingContainer;

                
string strDQAManualFolder = HttpContext.Current.Server.MapPath(@"~\DOC\Manual");
                
string strFileName = ((LinkButton)objGridViewRow.FindControl("lbn" + columnName)).Text;
                
string strGuid = ((Label)objGridViewRow.FindControl("lblGuid" + columnName)).Text;
                
string strFileType = ((Label)objGridViewRow.FindControl("lblType" + columnName)).Text;

                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.ContentType 
= "application/octet-stream";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader(
"Content-Disposition""attachment; filename=" + System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF7) + "");
                HttpContext.Current.Response.TransmitFile(strDQAManualFolder 
+ "\\" + strGuid + '.' + strFileType);
            }
            
catch (Exception ex)
            {
                
//Response.Write(ex.ToString());
            }
        }

        
void lbnFileName_DataBinding(object sender, EventArgs e)//LinkButton 数据绑定
        {
            LinkButton lbn 
= (LinkButton)sender;
            GridViewRow row 
= (GridViewRow)lbn.NamingContainer;
            lbn.Text 
= DataBinder.Eval(row.DataItem, columnName).ToString();
        }       
       
     }   

2. GridView 模版列的添加

 

ContractedBlock.gifExpandedBlockStart.gifCode
1 TemplateField customField;
2 customField = new TemplateField();
3 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, listDesc);
4 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, column);
5 this.gdvChnManual.Columns.Add(customField);
6 
7 //这里可以自由加列
8 this.gdvChnManual.DataSource = dt;
9 this.gdvChnManual.DataBind();

 

 

转载于:https://www.cnblogs.com/andycai/archive/2009/11/25/1610454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值