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

 這個例子值提供了一種思路,需要有其他功能的網友可以修改代碼為自己想要的表頭內容形式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM