您好,欢迎访问一九零五行业门户网

xml学习(5)xml配置gridview列

有时候我们不确定我们gridview要显示那些列,或者我们希望可以动态配置gridview那些列显示,以及宽度,这时可以通过可以把列的消息存放的数据库,通过读取数据库实现动态绑定列,也可以通过配置xml实现,当然也可以第一次通过读取xml,然后向数据库插入列消息,然后下一次判断数据库是否已经存储了列数据,如果没有存入,那么读取xml。
下面我讲解如何通过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学习(5)xml配置gridview列的内容。
其它类似信息

推荐信息