后台动态生成GridView列和模版
考虑到很多数据源是不确定的,所以这时无法在前台设置gridview的表头,需要在后台动态指定并绑定数据。
前台代码如下:
<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </asp:Content>
.cs页面代码如下
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BindData(); } /// <summary> /// 绑定gridview /// </summary> public void BindData() { string sqlcc = "SELECT * from Users";//order by gwbg.id desc DataTable dt = new DataTable(); dt = DBHelp.ExecuteQueryWithoutParameter(sqlcc, CommandType.Text); GridView1.AutoGenerateColumns = false;//设置自动产生列为false GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing); //添加编辑事件 GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);//添加删除事件 GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);//添加删除事件 GridViewBind(GridView1, dt, "Id"); } /// <summary> /// 绑定生成GridView /// </summary> /// <param name="gdv">要绑定的GridView</param> /// <param name="dtblDataSource">GridView的数据源</param> /// <param name="strDataKey">GridView的DataKeyNames</param> public static void GridViewBind(GridView gdv, DataTable dtblDataSource, string strDataKey) { gdv.Columns.Clear(); gdv.AutoGenerateColumns = false; gdv.DataSource = dtblDataSource; gdv.DataKeyNames = new string[]{ strDataKey }; for (int i = 0; i < dtblDataSource.Columns.Count; i++) //绑定普通数据列 { BoundField bfColumn = new BoundField(); bfColumn.DataField = dtblDataSource.Columns[i].ColumnName; bfColumn.HeaderText = dtblDataSource.Columns[i].Caption; gdv.Columns.Add(bfColumn); } //添加编辑列 CommandField cf = new CommandField();//命令字段 cf.ButtonType = ButtonType.Link;//超链接样式的按钮 cf.ShowEditButton = true;//显示编辑按钮 cf.CausesValidation = false;//引发数据验证为false cf.HeaderText = "编辑"; cf.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gdv.Columns.Add(cf);//添加编辑按钮到gridview //添加删除列 CommandField cf2 = new CommandField(); cf2.ButtonType = ButtonType.Link; cf2.ShowDeleteButton = true;//显示删除按钮 cf2.CausesValidation = false; cf2.HeaderText = "删除"; cf2.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gdv.Columns.Add(cf2); //添加包复选框的模板列 TemplateField tf = new TemplateField(); tf.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow); //tf.HeaderText = "选择"; tf.HeaderTemplate = new MyTemplate("模版列-选择框", DataControlRowType.Header); tf.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gdv.Columns.Add(tf); gdv.DataBind();//绑定 } //删除gridview的行 void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int i = e.RowIndex; MessageBox.Show(this, "删除ID为:"+GridView1.Rows[i].Cells [0].Text.ToString () ); } //编辑行 public void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { int i = e.NewEditIndex; string id = GridView1.Rows[i].Cells[0].Text.ToString(); MessageBox.Show(this, "删除ID为:" + id); } //取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; BindData(); } }
其中,添加模版的MyTemplate类代码为:
public class MyTemplate : ITemplate { private string strColumnName; private DataControlRowType dcrtColumnType; public MyTemplate() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 动态添加模版列 /// </summary> /// <param name="strColumnName">列名</param> /// <param name="dcrtColumnType">列的类型</param> 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: //模版列内容——加载CheckBox CheckBox cb = new CheckBox(); cb.ID = "CheckBox1"; cb.Checked = false; ctlContainer.Controls.Add(cb); break; } } }
在MyTemplate类中,这里是直接写了添加CheckBox,其实我们可以在MyTemplate的构造函数里添加一个参数,表示传进来的要在模版里面添加的控件的类型。这样就可以根据
控件类型来自动生成控件了,而不要在类里面去手动写需要生成的控件。
其中效果图如下: