小編給大家分享一下如何通過(guò)xml動(dòng)態(tài)綁定gridview,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)建站專(zhuān)注于古交網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供古交營(yíng)銷(xiāo)型網(wǎng)站建設(shè),古交網(wǎng)站制作、古交網(wǎng)頁(yè)設(shè)計(jì)、古交網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造古交網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供古交網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
如何通過(guò)xml如何動(dòng)態(tài)綁定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 { //數(shù)據(jù)行 int i = 0;//列標(biāo)志 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("內(nèi)容", "NO", 1); this.gv_class.DataSource = dt.DefaultView; this.gv_class.DataBind(); } }
表頭.xml
內(nèi)容.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 的摘要說(shuō)明 /// public class Functions { private static object _synRoot=new object(); private static Functions _instance=null; private Functions() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public static Functions getInstance() { lock (_synRoot) { if (_instance == null) { _instance = new Functions(); } } return _instance; } #region 業(yè)務(wù)層 ////// 綁定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 的摘要說(shuō)明 /// public class XmlHelper { private string rtnXml; public XmlHelper() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // 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 { //數(shù)據(jù)行 int i = 0;//列標(biāo)志 DataRow dr = result.NewRow(); foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } result.Rows.Add(dr); } } } return result; } /// /// /// /// xml名字 /// 序號(hào)列名字 /// 序號(hào)開(kāi)始編號(hào) ///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 { //數(shù)據(jù)行 int i = 1;//列標(biāo)志 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; } } }
看完了這篇文章,相信你對(duì)“如何通過(guò)xml動(dòng)態(tài)綁定gridview”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!