asp.Net中Gridview动态创建模板列
2012-02-08 14:11
597 查看
Web中有时候数据源字段不确定,需要在绑定后添加新的字段,有没有方法了?方法是有的:之前也在网上搜索过,找到了一些方法,通过模板列可以实现。这里借鉴一下,自己亲自试验了一下,达到了想要的效果。
Gridview动态添加模板列,下面就添加一空白列作为示例。
前台代码:
显示的界面如下:
那么,怎添加一列呢??动态添加列,关键是实现 ITemplate.InstantiateIn 方法。下面就自定义一个GridViewTemplate
因为之前遇到这样的问题:Gridview在绑定数据源后,发觉要添加新字段,即添加一列数据,而且要导出到Excel。按照一般的添加之后,导出Excel后发觉新添加的那列数据似乎并没有导出到Excel。如果用模板列的话,就可以解决上述的问题。
导出Excel代码如下:
如上述添加空白列后导出Excel截图如下:
叙述完毕,本文参考文章:GridView动态添加模板列,里面有对事件处理。值得参考
Gridview动态添加模板列,下面就添加一空白列作为示例。
前台代码:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %> <!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 runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="SID" HeaderText="学生ID" ReadOnly="True" /> <asp:BoundField DataField="SName" HeaderText="姓名" /> <asp:BoundField DataField="SAge" HeaderText="年龄" /> <asp:BoundField DataField="SSex" HeaderText="性别" /> <asp:BoundField DataField="SCollege" HeaderText="所属学院" /> <asp:CommandField HeaderText="选择" ShowSelectButton="True" /> </Columns> </asp:GridView> <br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </form> </body> </html>后台绑定数据库数据代码如下:
public static string ConnectionString = @"Data Source=.;Initial Catalog=StudentMSG;Persist Security Info=True;User ID=sa;Password=sa"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { // make the query string query = "SELECT * FROM Stable"; SqlConnection myConnection = new SqlConnection(ConnectionString); SqlDataAdapter ad = new SqlDataAdapter(query, myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "Stable"); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] { "SID" };//主键 GridView1.DataBind(); }
显示的界面如下:
那么,怎添加一列呢??动态添加列,关键是实现 ITemplate.InstantiateIn 方法。下面就自定义一个GridViewTemplate
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://可以定义自己想显示的控件以及绑定事件 break; default: break; } } }GridView模板定义好了。现在就可以添加新列,重写OnInit,代码如下:
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, "");//空白列 customField.ItemTemplate = gvt; GridView1.Columns.Add(customField); base.OnInit(e); }到此就可以显示新添加的列了。效果如下:
因为之前遇到这样的问题:Gridview在绑定数据源后,发觉要添加新字段,即添加一列数据,而且要导出到Excel。按照一般的添加之后,导出Excel后发觉新添加的那列数据似乎并没有导出到Excel。如果用模板列的话,就可以解决上述的问题。
导出Excel代码如下:
protected void Button1_Click(object sender, EventArgs e) { if (GridView1 != null) { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("订卷统计") + ".xls"); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); } else { this.Response.Write("<script language=javascript>alert('没有符合条件的记录!')</script>"); } } public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for }
如上述添加空白列后导出Excel截图如下:
叙述完毕,本文参考文章:GridView动态添加模板列,里面有对事件处理。值得参考
相关文章推荐
- asp.Net中Gridview动态创建模板列
- asp.Net中Gridview动态创建模板列 .
- asp.Net中Gridview动态创建模板列
- 动态创建 ASP.NET Web 服务器控件模板(出自MSDN)
- [导入]ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- asp.net动态添加GridView的模板列,并获取列值
- ASP.NET中动态创建DataGrid的模板列
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET中为DataGrid动态创建模板列
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子