2016-07-29 47 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>