首页 > 学院 > 开发设计 > 正文

GridView后台绑定数据列表方法

2019-11-17 03:16:56
字体:
来源:转载
供稿:网友

GridView后台绑定数据列表方法

在很多时候数据绑定都是知道了数据表中的表字段来绑定GridView控件的,那时候我就有个想法希望通过表明来查询数据库中的字段来动态的绑定GirdView控件数据并提供了相关的操作列,在网上找了一些资料字按照自己的想法改进写了一个后台绑定GridView控件得得模板。其中最主要的好处是只需要知道数据库中的列名就可以了,表头可以在一个其他文件中来和数据列表绑定 /* * 2014-02-27 * GridView数据列绑定帮助文档 * 用于动态添查询数据绑定到数据表中 * 提供各种类型的绑定 *  * **/using System;using System.Collections.Generic;using System.Linq;using System.Text;//using System.Web.UI;using System.Web.UI.WebControls;namespace CommonLib{    /// <summary>    /// GridView数据列绑定中兴    /// </summary>    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;        private xmlTableInfo xmTable = null;        private BindType bind = BindType.label;        public GridViewTemplate()        { }        /// <summary>        /// 构造函数        /// </summary>        /// <param name="type">绑定列类型</param>        /// <param name="colname">绑定列名称或者需要绑定的数据库字段</param>        /// <param name="colname">绑定字段类型控件</param>        public GridViewTemplate(DataControlRowType type, string colname, BindType bin, XMLTableInfo tab)        {            templateType = type;            columnName = colname;            bind = bin;            this.xmTable = tab;        }        /// <summary>        /// 绑定事件        /// </summary>        /// <param name="type"></param>        /// <param name="controlID"></param>        /// <param name="colname"></param>        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://标题绑定                    if (bind == BindType.label)                    {                        Literal lc = new Literal();                        lc.Text = columnName;                        container.Controls.Add(lc);                    }                    if (bind == BindType.checkbok)                    {                        Literal lc = new Literal();                       //可以按照自己想要处理方法来写                        lc.Text = "<input type='checkbox' class='heck_box' />全选";                        container.Controls.Add(lc);                    }                    if (bind == BindType.editor)                    {                        Literal lc = new Literal();                        lc.Text = "编辑";                        container.Controls.Add(lc);                    }                    if (bind == BindType.delete)                    {                        Literal lc = new Literal();                        lc.Text = "";// "删除";                        container.Controls.Add(lc);                    }                    break;                case DataControlRowType.DataRow://普通列绑定                    if (bind == BindType.label)                    {                        Label tb = new Label();                        tb.DataBinding += tb_DataBinding;                        container.Controls.Add(tb);                    }                    if (bind == BindType.checkbok)                    {                        Literal lic = new Literal();                        lic.DataBinding += lic_DataBinding;                        container.Controls.Add(lic);                    }                    if (bind == BindType.editor)                    {                        Literal lec = new Literal();                        lec.DataBinding += lec_DataBinding;                        container.Controls.Add(lec);                    }                    if (bind == BindType.editor)                    {                        Literal del = new Literal();                        del.DataBinding += del_DataBinding;                        container.Controls.Add(del);                    }                    break;                default:                    break;            }        }        void del_DataBinding(object sender, EventArgs e)        {            Literal tb = (Literal)sender;            GridViewRow row = (GridViewRow)tb.NamingContainer;            tb.ID = columnName;            //可以按照自己想要处理方法来写            tb.Text = " &nbsp;&nbsp;<a class='A_DelBind' href='Ajax/AjaxDateToSql.ashx?cmd=delete&cName=" + xmTable.CName + "&eName=" + xmTable.EName + "&id=" + DataBinder.Eval(row.DataItem, columnName).ToString() + "'>删除</>";        }        void lec_DataBinding(object sender, EventArgs e)        {            Literal tb = (Literal)sender;            GridViewRow row = (GridViewRow)tb.NamingContainer;            tb.ID = columnName;            //可以按照自己想要处理方法来写            tb.Text = "<a href='InsertInfo.aspx?cmd=editor&cName=" + xmTable.CName + "&eName=" + xmTable.EName + "&id=" + DataBinder.Eval(row.DataItem, columnName).ToString() + "'>编辑</>";        }        void lic_DataBinding(object sender, EventArgs e)        {            Literal btn = (Literal)sender;            GridViewRow row = (GridViewRow)btn.NamingContainer;            btn.ID = columnName;//可以按照自己想要处理方法来写此处绑定一个复选框 类名为CheckBox            btn.Text = "<input type='checkbox' class='CheckBox' value='" + DataBinder.Eval(row.DataItem, columnName).ToString() + "'/>";        }        /// <summary>        /// 2014-02-27        /// 张国强        /// 绑定字段数据列事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void tb_DataBinding(object sender, EventArgs e)        {            Label tb = (Label)sender;            try            {                GridViewRow row = (GridViewRow)tb.NamingContainer;                tb.ID = columnName;                tb.Text = DataBinder.Eval(row.DataItem, columnName).ToString();            }            catch (Exception)            {                                             }                   }    }    /// <summary>    /// 模板类型枚举    /// </summary>    public enum BindType    {        label, checkbok, editor, delete    }}

调用方法例字

  /// <summary>        /// 绑定数据到GridView控件        /// </summary>        /// <param name="NewGrid">GridViews控件</param>        /// <param name="eName">英文名称</param>        /// <param name="cName">中文名称</param>        /// <param name="sqlOrTop">需要查询的数据字符串</param>        public  void LoadInfo(GridView NewGrid, string eName, string cName,string sqlOrTop)        {            NewGrid.Columns.Clear();            XMLTableInfo tb = new XMLTableInfo();            tb.CName = cName;            tb.EName = eName;            XMLBase xBase = new XMLBase();            xBase.LoadDome();            string tableInfo = xBase.Check(eName, cName);            if (tableInfo=="")            {                return;            }            //1,拆分数据            string[] Ename = tableInfo.Split('¦')[0].Split(',');            string[] Cname = tableInfo.Split('¦')[1].Split('[');            xBase.LoadDome();            string id = xBase.GetIdentity(eName, cName);            //循环数表中的列名            for (int i = 0; i < Ename.Length; i++)            {                               TemplateField temp = new TemplateField();                temp.ShowHeader = true;                if (i == 0)                {                    temp.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, Cname[i], Bind
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表