在vb.net中按日期过滤和排序xml数据

马其顿零

最终,我试图找到一种在gridview显示中成功排序和过滤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"),读入的数据都是date列中的字符串,因此,我可以找到对它们进行排序的唯一方法是通过像这样更改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”子句的方法,但未成功

编辑:将部分标记为代码,但我忘记标记为代码

马其顿零

我找到了解决我问题的方法。我需要使用数据视图而不是数据集来更改,排序和过滤我的数据。

我的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模式,以确保将date列作为日期写入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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章