亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

?如何通過xml動態綁定gridview

發布時間:2021-01-28 10:09:31 來源:億速云 閱讀:168 作者:小新 欄目:編程語言

小編給大家分享一下如何通過xml動態綁定gridview,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

如何通過xml如何動態綁定gridview

一效果:

?如何通過xml動態綁定gridview

二,核心代碼:

1.讀取xml文件,返回datatable

    /// <summary>
        /// 獲取xmltable
        /// </summary>
        /// <param name="xmlname">xml名字</param>
        /// <returns></returns>
        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列

    /// <summary>
    /// 綁定gridview列
    /// </summary>
    /// <param name="gv"></param>
    /// <param name="dt"></param>
    /// <param name="width"></param>
    /// <param name="columnNumber"></param>
    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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>讀取xml</title>
    <link rel="Stylesheet" type="text/css" href="Css/xmlReadOne.css" />
    <link rel="Stylesheet" type="text/css" href="Css/Common/InputStyle.css" />
    <script type="text/javascript"  src="Scripts/jquery-1.4.1.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <p>
    <p class="main">
      <p class="list">
        <asp:GridView runat="server" ID="gv_class" AutoGenerateColumns="False"  
              DataKeyNames="ID" Width="100%" CellPadding="4"  
              ForeColor="#333333" GridLines="None"   >
            <AlternatingRowStyle BackColor="White" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6"  HorizontalAlign="Center" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
            <Columns>
              <asp:TemplateField   HeaderText="NO">
                <ItemTemplate>
                  <%#Eval("NO") %>
                </ItemTemplate>
              </asp:TemplateField>
            </Columns>
        </asp:GridView>
      </p>
     </p>
    </p>
    </form>
</body>
 <script type="text/javascript">
     function add_clear() {
         $(".button").find("input[type=text] ").each(function () {
             $(this).val('');
         });
     }
    </script>
</html>


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();
        }
    }


    /// <summary>
    /// 綁定gridview
    /// </summary>
    private void InitGV()
    {
        DataTable dt = xmlHelper.getDTXML("內容", "NO", 1);
        this.gv_class.DataSource = dt.DefaultView;
        this.gv_class.DataBind();
    }

}


表頭.xml

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <Header  col1="name"      col2="field_code" col3="width" col4="dispaly"  ></Header>
    <row     col1="班級編號"  col2="ID"         col3="100"   col4="true"     ></row>
    <row     col1="班級"      col2="class"      col3="100"   col4="true"     ></row>
    <row     col1="班級名稱"  col2="class_name" col3="100"   col4="true"     ></row>
    <row     col1="年級"      col2="year"       col3="100"   col4="true"     ></row>
    <row     col1="學校"      col2="school"     col3="100"   col4="true"     ></row>
    <row     col1="人數"      col2="count"     col3="100"    col4="true"     ></row>
</config>



內容.xml

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <Header  col1="ID" col2="class" col3="class_name" col4="year" col5="school"  col6="count" ></Header>
  <row col1="一五班" col2="01" col3="實驗班" col4="2013" col5="三峽高級中學"  col6="20" ></row>
  <row col1="一六班" col2="02" col3="奧數班" col4="2013" col5="三峽高級中學"  col6="25" ></row>
  <row col1="一六班" col2="03" col3="陽光班" col4="2013" col5="三峽高級中學"  col6="69" ></row>
</config>


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;

/// <summary>
///Server 的摘要說明
/// </summary>
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 業務層
    /// <summary>
    /// 綁定gridview列
    /// </summary>
    /// <param name="gv"></param>
    /// <param name="dt"></param>
    /// <param name="width"></param>
    /// <param name="columnNumber"></param>
    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
{
   

    /// <summary>
    ///XmlHelper 的摘要說明
    /// </summary>
    public class XmlHelper
    {


        private string rtnXml;
        public XmlHelper()
        {
            //
            //TODO: 在此處添加構造函數邏輯
            //
            rtnXml = string.Empty;
        }

        /// <summary>
        /// 獲取xmltable
        /// </summary>
        /// <param name="xmlname">xml名字</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlname">xml名字</param>
        /// <param name="Header">序號列名字</param>
        /// <param name="colsNumber">序號開始編號</param>
        /// <returns></returns>
        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”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汉中市| 墨竹工卡县| 绥德县| 精河县| 察隅县| 邯郸市| 鹿泉市| 汾阳市| 宁河县| 德江县| 马关县| 于都县| 磐石市| 东乌| 汉川市| 海宁市| 顺义区| 电白县| 泰兴市| 扶绥县| 湘西| 巫山县| 昌江| 逊克县| 广丰县| 留坝县| 乐亭县| 通道| 子长县| 浪卡子县| 隆林| 宜良县| 丰顺县| 兴宁市| 嘉善县| 启东市| 涿州市| 旅游| 德格县| 莒南县| 根河市|