​如何通过xml动态绑定gridview

小编给大家分享一下如何通过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
    
    
    


    
    

                                                                                                                                                                                                                      <%#Eval("NO") %>                                                             

     

    

            function add_clear() {          $(".button").find("input[type=text] ").each(function () {              $(this).val('');          });      }     


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://bzwzjz.com/article/jhjpep.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 外贸营销网站建设 定制级高端网站建设 成都网站建设 网站制作公司 成都企业网站制作 网站制作 泸州网站建设 网站建设 网站建设 成都做网站建设公司 成都网站设计 成都网站建设推广 成都网站设计 成都网站建设公司 定制网站建设多少钱 移动网站建设 高端网站设计 网站制作 四川成都网站制作 成都品牌网站建设 手机网站建设套餐 成都网站设计