最終,我試圖找到一種方法,可以在gridview顯示中成功排序和過濾我的xml數據。在vb.net中按日期過濾和排序xml數據
我有一個XML表是這樣的:
<?xml version="1.0" standalone="yes"?>
<Notification>
<Info>
<Event>Template</Event>
<Date>1899/01/01</Date>
</Info>
<Info>
<Event>picnic</Event>
<Date>2016/07/15</Date>
</Info>
<Info>
<Event>party</Event>
<Date>2015/10/29</Date>
</Info>
</Notification>
,我需要篩選和排序成兩個不同的頁面一個asp.net的GridView。在一頁上,我記錄了所有事件,並允許添加,更新和刪除記錄。第二個gridview位於我的主頁的角落,應該顯示當前/即將發生的事件。我定義它們像這樣(注:第二個具有更小的尺寸大小,但是這是唯一的區別):
<asp:GridView ID="GridView1" runat="server" HeaderStyle-ForeColor="#FF5A09" RowStyle-ForeColor="#FF9900"
AutoGenerateColumns="false" BorderWidth="2px"
Width="1294px" Height="350px" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" AllowSorting="true" >
<Columns >
<asp:BoundField DataField="Event" HeaderText="Event" ItemStyle-Width="150" />
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="150" />
<asp:CommandField ShowEditButton="True" ItemStyle-Width="30"/>
<asp:CommandField ShowDeleteButton="True" ItemStyle-Width="30"/>
</Columns>
我使用這個vb.net功能到我的XML數據綁定到我的GridView
Private Sub BindGrid()
Dim ds As New DataSet
ds.ReadXml(Server.MapPath("~/Event_Info.xml"))
GridView1.DataSource = ds
GridView1.DataBind()
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub
我的問題是,無論何時我讀取xml數據,或嘗試使用Dim doc as XDocument=XDocument.Load("Path to my xml")
加載它時,讀入的數據都是日期列中的一個字符串,所以我唯一能找到的方法是改變我的BindGrid()功能如下:
Private Sub BindGrid()
Dim ds As New DataSet
ds.Tables[0].DefaultView.Sort = "Date desc"
ds.ReadXml(Server.MapPath("~/Event_Info.xml"))
GridView1.DataSource = ds.Tables[0].DefaultView
GridView1.DataBind()
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub
這使我排序,如果我只輸入日期yyyy/mm/dd,但我的添加,刪除和更新功能不再有效。
,如果你想看到他們,我會張貼在這裏,但你也許可以跳過此位:本Button_click在此間舉行的頂部添加一條記錄到GridView
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BindGrid()
Dim oDs As DataSet = GridView1.DataSource
Dim oDr As DataRow = oDs.Tables(0).NewRow
oDr("Event") = TextBox1.Text
oDr("Date") = TextBox2.Text
oDs.Tables(0).Rows.Add(oDr)
oDs.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml")
BindGrid()
TextBox1.Text = String.Empty
TextBox2.Text = String.Empty
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
BindGrid()
Dim oDs As DataSet = GridView1.DataSource
oDs.Tables(0).Rows(GridView1.Rows(e.RowIndex).DataItemIndex).Delete()
oDs.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml")
BindGrid()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
BindGrid()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
BindGrid()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
' Get the new values from the GridView controls
Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
Dim n As String = CType(GridView1.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
Dim rn As String = CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
GridView1.EditIndex = -1
BindGrid()
' Update the XML file using the new values
Dim oDs As DataSet = GridView1.DataSource
oDs.Tables(0).Rows(i).Item(0) = n
oDs.Tables(0).Rows(i).Item(1) = rn
oDs.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml")
BindGrid()
End Sub
結束跳過位
我也不知道如何成功過濾主頁上的數據,以便它只顯示當前和未來的事件。我試圖想辦法應用排序上的日期的「where」子句,但並不成功
編輯:標節,如代碼,我忘了標記爲代碼
首先將模式寫入xml文件,以便它將Date作爲實際日期。然後,當你讀取文件時,它也會是Date:oDs.WriteXml(Request.PhysicalApplicationPath +「Event_Info.xml」,XmlWriteMode.WriteSchema)。還修復GridView1_RowUpdating,以便您將DateTime寫入第二列而不是字符串。該文本框應該是一個字符串bu,而不是DataTable或DGV。 – jdweng
@jdweng謝謝,這幫助我弄清楚瞭如何寫入模式,併爲我解決了一些解決這個問題的一些步驟。 – MacedonZero