大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下如何通过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”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。