2016-07-29 72 views
0

最終,我試圖找到一種方法,可以在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」子句,但並不成功

編輯:標節,如代碼,我忘了標記爲代碼

+0

首先將模式寫入xml文件,以便它將Date作爲實際日期。然後,當你讀取文件時,它也會是Date:oDs.WriteXml(Request.PhysicalApplicationPath +「Event_Info.xml」,XmlWriteMode.WriteSchema)。還修復GridView1_RowUpdating,以便您將DateTime寫入第二列而不是字符串。該文本框應該是一個字符串bu,而不是DataTable或DGV。 – jdweng

+0

@jdweng謝謝,這幫助我弄清楚瞭如何寫入模式,併爲我解決了一些解決這個問題的一些步驟。 – MacedonZero

回答

0

我找到了解決辦法對我的問題。我需要使用數據視圖而不是數據集來更改,排序和過濾數據。

我bindgrid功能成爲本:

Private Sub BindGrid() 

     Dim ds As New DataSet 
     ds.ReadXml(Server.MapPath("~/Event_Info.xml")) 
     Dim myView As New DataView 

     myView = ds.Tables(0).DefaultView 

     myView.Sort = "Date desc" 

     GridView1.DataSource = myView 

     GridView1.DataBind() 


    End Sub 

注意這個較大的改變意味着,我的添加/刪除/更新功能不得不改變。我最後寫他們這樣的,如果有人想看到他們:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     BindGrid() 
     Dim dv As DataView = GridView1.DataSource 
     Dim oDr As DataRowView = dv.AddNew() 
     oDr("Event") = TextBox1.Text 
     oDr("Date") = TextBox2.Text 
     oDr.EndEdit() 

     dv.DataViewManager.DataSet.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml", XmlWriteMode.WriteSchema) 
     dv.Sort = "Date desc" 
     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 oDv As DataView = GridView1.DataSource 
     oDv.Delete(GridView1.Rows(e.RowIndex).RowIndex) 
     oDv.DataViewManager.DataSet.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml", XmlWriteMode.WriteSchema) 
     oDv.Sort = "Date desc" 
     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 oDv As DataView = GridView1.DataSource 
     oDv.DataViewManager.DataSet.Tables(0).Rows(i).Item(0) = n 
     oDv.DataViewManager.DataSet.Tables(0).Rows(i).Item(1) = rn 
     oDv.DataViewManager.DataSet.WriteXml(Request.PhysicalApplicationPath + "Event_Info.xml", XmlWriteMode.WriteSchema) 
     BindGrid() 
    End Sub 

當然,作爲意見建議,我定義的XML架構,以確保日期欄將被寫入/讀取的xml數據作爲日期

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="EventsSchema" 
    targetNamespace="http://tempuri.org/EventsSchema.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/EventsSchema.xsd" 
    xmlns:mstns="http://tempuri.org/EventsSchema.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 

    <xs:element name="Notification"> 
    <xs:complexType> 
     <xs:sequence> 

     <xs:element name="Info" minOccurs="0" maxOccurs="unbounded"> 
      <xs:complexType> 
      <xs:sequence> 

       <xs:element name="Event" type="xs:string"></xs:element> 
       <xs:element name="Date" type="xs:dateTime"></xs:element> 

      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 

     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema>