如何通过xml动态绑定gridview
小编给大家分享一下如何通过xml动态绑定gridview,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联建站专注于古交网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供古交营销型网站建设,古交网站制作、古交网页设计、古交网站官网定制、小程序设计服务,打造古交网络公司原创品牌,更为您提供古交网站排名全网营销落地服务。
如何通过xml如何动态绑定gridview
一效果:
二,核心代码:
1.读取xml文件,返回datatable
////// 获取xmltable /// /// xml名字 ///public DataTable getDTXML(string xmlname) { DataTable result = new DataTable(); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { // //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 0;//列标志 DataRow dr = result.NewRow(); foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } result.Rows.Add(dr); } } } return result; }
2.绑定gridview列
////// 绑定gridview列 /// /// /// /// /// public void HtmlGridView(GridView gv,DataTable dt,int width,int columnNumber) { try { int k=0; if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { BoundField bf = new BoundField(); bf.HeaderText = dr["name"].ToString(); bf.DataField = dr["field_code"].ToString(); bf.HeaderStyle.Width = Convert.ToInt32(dr["width"].ToString()); bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; bf.HeaderStyle.ForeColor = System.Drawing.Color.Black; bf.SortExpression = dr["name"].ToString(); gv.Columns.Add(bf); k+= Convert.ToInt32(dr["width"].ToString()); if (dr["width"].ToString()=="0") { gv.Columns[columnNumber].Visible = false; } columnNumber++; } gv.Width = k + width; } } catch(Exception ex) { throw new Exception(ex.Message); } }
三,下面是全部源码:
HtmlGridView.aspx源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HtmlGridView.aspx.cs" Inherits="HtmlGridView" %>读取xml
HtmlGridView.aspx.cs源码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Xml; using System.Web.UI.HtmlControls; using IsXMLUtility; using System.Data; public partial class HtmlGridView : System.Web.UI.Page { XmlHelper xmlHelper = new XmlHelper(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //gv_class绑定列 DataTable dt = xmlHelper.getDTXML("表头"); Functions.getInstance().HtmlGridView(gv_class, dt, 10, 1); InitGV(); } } ////// 绑定gridview /// private void InitGV() { DataTable dt = xmlHelper.getDTXML("内容", "NO", 1); this.gv_class.DataSource = dt.DefaultView; this.gv_class.DataBind(); } }
表头.xml
内容.xml
xmlReadOne.css
body{ margin:0 auto; padding:0px; font-family:'宋体'; } .main { margin-left:20px; margin-top:10px; text-align:left; } .button { width:600px; border:#A8B7CC solid 1px; background-color:#FFFFFF; } .content { width:600px; border:#A8B7CC solid 1px; background-color:#E0EDFE; margin-top:20px; } .list { width:600px; height:300px; overflow:auto; border:#A8B7CC solid 1px; background-color:#FFFFFF; overflow: scroll; SCROLLBAR-FACE-COLOR:#E0EDFE; PADDING-BOTTOM: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR:#ffffff; SCROLLBAR-ARROW-COLOR:#95AFD4; PADDING-TOP: 0px; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; LETTER-SPACING: 1pt; margin-top:20px; } .gv { width:600px; height:400px; margin-top:20px; border:#A8B7CC solid 1px; background-color:#FFFFFF; overflow: scroll; SCROLLBAR-FACE-COLOR:#E0EDFE; PADDING-BOTTOM: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR:#ffffff; SCROLLBAR-ARROW-COLOR:#95AFD4; PADDING-TOP: 0px; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; LETTER-SPACING: 1pt; }
Functions.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Web.UI.WebControls; //////Server 的摘要说明 /// public class Functions { private static object _synRoot=new object(); private static Functions _instance=null; private Functions() { // //TODO: 在此处添加构造函数逻辑 // } public static Functions getInstance() { lock (_synRoot) { if (_instance == null) { _instance = new Functions(); } } return _instance; } #region 业务层 ////// 绑定gridview列 /// /// /// /// /// public void HtmlGridView(GridView gv,DataTable dt,int width,int columnNumber) { try { int k=0; if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { BoundField bf = new BoundField(); bf.HeaderText = dr["name"].ToString(); bf.DataField = dr["field_code"].ToString(); bf.HeaderStyle.Width = Convert.ToInt32(dr["width"].ToString()); bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; bf.HeaderStyle.ForeColor = System.Drawing.Color.Black; bf.SortExpression = dr["name"].ToString(); gv.Columns.Add(bf); k+= Convert.ToInt32(dr["width"].ToString()); if (dr["width"].ToString()=="0") { gv.Columns[columnNumber].Visible = false; } columnNumber++; } gv.Width = k + width; } } catch(Exception ex) { throw new Exception(ex.Message); } } #endregion }
XmlHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Xml; using System.IO; using System.Web.UI; using System.Web.UI.WebControls; namespace IsXMLUtility { //////XmlHelper 的摘要说明 /// public class XmlHelper { private string rtnXml; public XmlHelper() { // //TODO: 在此处添加构造函数逻辑 // rtnXml = string.Empty; } ////// 获取xmltable /// /// xml名字 ///public DataTable getDTXML(string xmlname) { DataTable result = new DataTable(); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { // //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 0;//列标志 DataRow dr = result.NewRow(); foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } result.Rows.Add(dr); } } } return result; } /// /// /// /// xml名字 /// 序号列名字 /// 序号开始编号 ///public DataTable getDTXML(string xmlname, string Header, int colsNumber) { DataTable result = new DataTable(); if (Header == "") Header = "N0"; if (colsNumber < 0) colsNumber = 1; result.Columns.Add(Header); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 1;//列标志 DataRow dr = result.NewRow(); dr[0] = colsNumber; foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } colsNumber++; result.Rows.Add(dr); } } } return result; } } }
看完了这篇文章,相信你对“如何通过xml动态绑定gridview”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
新闻标题:如何通过xml动态绑定gridview
网页路径:http://ybzwz.com/article/jhjpep.html