六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

XML文件绑定到不同的DataGrid的方法

[复制链接]

升级  16.15%

427

主题

427

主题

427

主题

探花

Rank: 6Rank: 6

积分
1323
 楼主| 发表于 2013-1-26 14:25:57 | 显示全部楼层 |阅读模式
将某一目录下的所有相同格式的 XML文件绑定到不同的DataGrid的方法。
<xmp>Sub Page_Load( sender as object,  e as System.EventArgs)  Dim dir As DirectoryInfo = New DirectoryInfo("D:\Web")  Dim files As FileInfo() = dir.GetFiles()  Dim count As Integer = files.Length  Dim i As Integer  For i = 0 To count - 1    If files(i).Name.SubString(files(i).Name.LastIndexOf(".")) = ".xml" Then      Dim ds As New DataSet()      'ds.ReadXml("d:\Web\c.xml")      ds.ReadXml(files(i).FullName)      Dim dt as DataGrid = New DataGrid()      dt.ID = "DataGrid" + i.ToString()      dt.AutoGenerateColumns=false      Dim MyName As BoundColumn = New BoundColumn()      Dim MyProductID As BoundColumn = New BoundColumn()      Dim Price As BoundColumn = New BoundColumn()      Dim Quantity As BoundColumn = New BoundColumn()      MyName.HeaderText="名字"      MyName.DataField="Name"      MyProductID.HeaderText="序号"      MyProductID.DataField="ProductID"      Price.HeaderText="价格"      Price.DataField="Price"      Quantity.HeaderText="数量"      Quantity.DataField="Quantity"      dt.Columns.AddAt(0, MyName)      dt.Columns.AddAt(1, MyProductID)      dt.Columns.AddAt(2, Price)      dt.Columns.AddAt(3, Quantity)      dt.DataSource = ds.Tables("Product")      dt.DataBind()      Me.Controls.Add(dt)    End If    NextEnd Sub</xmp>C#写法
<xmp>void Page_Load(object sender, System.EventArgs e){  DirectoryInfo dir  = new DirectoryInfo("D:\\Web");  FileInfo[] files = dir.GetFiles();  int count = files.Length;  for(int i = 0;i<count if dataset ds="new" ds.readxml datagrid dt="new" dt.id="DataGrid" i.tostring dt.autogeneratecolumns="false;" boundcolumn myname="new" myproductid="new" price="new" quantity="new" myname.headertext="名字" myname.datafield="Name" myproductid.headertext="序号" myproductid.datafield="ProductID" price.headertext="价格" price.datafield="Price" quantity.headertext="数量" quantity.datafield="Quantity" dt.columns.addat dt.datasource='ds.Tables["Product"];' dt.databind this.controls.add><form runat="server"></form></count></xmp>xml文件格式:
<xmp><?xml version="1.0" encoding="gb2312"?><dataset><product><name>[孟宪会之精彩世界]</name><productid>1</productid><price>12000</price><quantity>1</quantity></product><product><name>http://dotnet.aspx.cc</name><productid>2</productid><price>12000</price><quantity>2</quantity></product><product><name>http://xml.sz.luohuedu.net/xml/</name><productid>3</productid><price>18000</price><quantity>2</quantity></product></dataset></xmp>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表