• 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], BindType.checkbok, tb);
                        temp.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, id, BindType.checkbok, tb);
                    }
                    else
                    {
                        temp.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, Cname[i], BindType.label, tb);
                        temp.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, Ename[i], BindType.label, tb);
                    }
    
    
                    NewGrid.Columns.Add(temp);
                }
                //添加编辑列
                TemplateField tempEditor = new TemplateField();
                tempEditor.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "操作", BindType.editor, tb);
                tempEditor.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, id, BindType.editor, tb);
                NewGrid.Columns.Add(tempEditor);
    
                TemplateField tempDelete = new TemplateField();
                tempDelete.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "删除", BindType.delete, tb);
                tempDelete.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, id, BindType.delete, tb);
                NewGrid.Columns.Add(tempDelete);
    
                string sql = "";
                if (sqlOrTop.Length<11)
                {
                    sql = "select top " + sqlOrTop + " * from " + eName + "  order by  " + id + " DESC";
                    NewGrid.DataSource = ExcelHelper.GetDataSet(sql);
                }
                else
                {
                    NewGrid.DataSource = ExcelHelper.GetDataSet(sqlOrTop);
                }
               
                NewGrid.AutoGenerateColumns = false;
                NewGrid.DataBind();
    
    
            }
    View Code

     这个例子值提供了一种思路,需要有其他功能的网友可以修改代码为自己想要的表头内容形式

  • 相关阅读:
    X 如何理解关系型数据库的常见设计范式?
    X 使用DMV,诊断和调优DB性能。
    X MSSQL-并发控制-2-Isolation msql 的各种隔离级别 sqlserver
    X SQL Server AG集群启动不起来的临时自救大招
    X 搭建非域AlwaysOn win2016+SQL2016
    X 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    X 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    X 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    Python Cookie Session和分页
    Python django应用之corsheaders[跨域设置]
  • 原文地址:https://www.cnblogs.com/imeiba/p/3611010.html
Copyright © 2020-2023  润新知