发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
在很多时候数据绑定都是知道了数据表中的表字段来绑定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 = " <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(); }
这个例子值提供了一种思路,需要有其他功能的网友可以修改代码为自己想要的表头内容形式
来自: 昵称10504424 > 《工作》
0条评论
发表
请遵守用户 评论公约
Creating Web Server Control Templates Dynamically
Creating Web Server Control Templates Dynamically.‘ Visual Basic Dim lc As New Literal() Case ListItemType.Item lc.Text = "<TR><TD>" AddHandler lc.DataBinding, Addr...
gridview动态添加、删除模版列
case ListItemType.下面是在 DataGrid 控件中使用动态模板与在 Repeater 或 DataList 控件中使用动态模板的区别: 请不要为网格本身创建项模板,而是为网格中的列创建项模板。Add(lc) ...
gridview导出到Excel 问题
导出数字问题和和导出时候runat='saver'或者数字类型导出不正确问题
如何知道点GridView中的哪一行
如何知道点GridView中的哪一行。很多朋友问,点击GridView中自定义的一个模版内的控件,如何知道是点击的哪一行。比如我们自定义了一个LinkButton,那么当我们点击这个控件时如何知道它在GridView中所...
根据Gridview 中的LinkButton获取当前按钮所在行索引
GridView的常用操作
GridView和DataGrid功能相似,都是在web页面中显示数据源中的数据,将数据源中的一行数据,也就是一条记录,显示为在web页面上输出表格中的一行。GridView和DataGrid在事件模型上也多有不同之处,DataG...
理解GridView控件
理解GridView控件gridviewGridView <asp:GridView ID="GridView3" ></asp:GridView> .net 中的服务器控件1. GridView控件的属 性表10.6 GridView控件的行为属性。表10.7 G...
RowDataBound事件
RowDataBound事件【转】RowDataBound事件(2009-02-12 14:43:24)转载标签: it 分类: 技术类 【转】RowDataBound事件2008-12-25 14:22RowDataBound事件在创建gridView控件时,必须先为GridView的每一...
asp.net读取GridView中某行或某单元格值
asp.net读取GridView中某行或某单元格值TextBox10.Text = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;试试 谢谢阿多的回答,试过了,一样的结果.本来Text就可以省略掉的吧...up单元格里是不是还...
微信扫码,在手机上查看选中内容