We help IT Professionals succeed at work.

Sort Gridview Data

Medium Priority
371 Views
Last Modified: 2012-05-12
Hi,

I am here yet again with tales of woe surrounding XML.... (by the end of this little task I should be perfect on XML...)

todays query is....

I currently have a gridview that is databound to an XML file which pulls through the information i want perfectly fine using the below code sample.

An afterthought of mine with this task was to merg the 6 XML files into one file and then render this XML file accordingly and again this works perfectly until I look at the times the records were created.  

What I would like to happen is for my dataset to be able to sort the XML records and render them in time order.

I have tried using the  AllowSorting="true" tag but when I build and run the page I get an error saying The  " GridView 'datagrid1' fired event Sorting which wasn't handled. "
Can anyone help by showing me what I need to do in order to sort the data or if there is another way of doing things...


Dim ds As New DataSet()
        If File.Exists("\\wintdc02\PUBLIC\TarehouseXML\" & machine.Text & year.Text & month.Text & day.Text & ".xml") Then
            ds.ReadXml("\\wintdc02\PUBLIC\TarehouseXML\" & machine.Text & year.Text & month.Text & day.Text & ".xml")
            datagrid1.DataSource = ds
            datagrid1.DataBind()
        Else
            Response.Write("The file you are looking for does not exist or has been moved")
        End If

Open in new window

Comment
Watch Question

Commented:
You need to wire up the Sorting even to the grid view. In the handler, you pull in the XML, sort it, and finally bind the sorted data to the gridview.

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   //Sort XML and bind to datagrid1
}

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
codecruiser, thats for .net 4 framework, where im working in 3.5...


strickdd, can you show me a full example in vb.net please?? Ive no idea on how to do it..
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
See that Other Versions drop down? Use that.


Capture.JPG

Author

Commented:
Hi,

Im just not understanding the microsoft examples... however correct they maybe the examples are not clear to me...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
Hi Code Cruiser,

I have tried both examples now and followed what I believe perfectly... and still get the same error.

The first example doesnt work for me because thats for SQL Bound Data, and if I were binding my gridview to this with a single specified datasource then this would allow me to sort.

The second example, comes up with the same error...

I have also looked at using a single XML file and using both a LINQ and XML datasource and neither of these give me the option of sorting....  so am at the moment stumped!!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
If you had followed any of the examples, they both handle the sorted event so you would not get the same error. Show me your code.

Author

Commented:
Code attached
Imports System.Data

Partial Public Class sort
    Inherits System.Web.UI.Page

    Public Property TimeSortDirection() As String
        Get
            Return ViewState("Time")
        End Get
        Set(ByVal value As String)
            ViewState("Time") = value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Me.TimeSortDirection = "ASC"
        End If
        Dim ds As New DataSet()

        ds.ReadXml("\\wintdc02\PUBLIC\TarehouseXML\BSUGBLB017032011111.xml")
        GridView1.DataSource = ds
        GridView1.DataBind()

    End Sub

    Protected Sub GridView1_Sorting(ByVal sender As Object, _
                                    ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        If e.SortExpression = "Time" Then
            If Me.TimeSortDirection = "ASC" Then
                e.SortExpression = "Time DESC"
            Else
                e.SortExpression = "Time ASC"
                Me.TimeSortDirection = "ASC"

            End If

        End If
    End Sub


End Class

Open in new window

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sort.aspx.vb" Inherits="XMLParser_Tarehouse.sort" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:GridView runat="server" AutoGenerateColumns="false" AllowSorting="true"
            ID="GridView1" ShowFooter="true" >
        <Columns>
        <asp:BoundField DataField="StationID" HeaderText="StationID" SortExpression="StationID"/>
        <asp:BoundField DataField="Date" HeaderText="Date"  SortExpression="Date"/>
        <asp:BoundField DataField="Time" HeaderText="Time"  SortExpression="Time"/>
        <asp:BoundField DataField="Stub" HeaderText="Stub"  SortExpression="Stub"/>
        <asp:BoundField DataField="DirtyWeight" HeaderText="DirtyWeight"  SortExpression="DirtyWeight"/>
        <asp:BoundField DataField="CleanWeight" HeaderText="CleanWeight"  SortExpression="CleanWeight"/>
        <asp:BoundField DataField="TopsWeight" HeaderText="TopsWeight"  SortExpression="TopsWeight"/>
        <asp:BoundField DataField="SugarValue" HeaderText="SugarValue"  SortExpression="SugarValue"/>
        <asp:BoundField DataField="AminoValue" HeaderText="AminoValue"  SortExpression="AminoValue"/>
        <asp:BoundField DataField="SodiumValue" HeaderText="SodiumValue"  SortExpression="SodiumValue"/>
        <asp:BoundField DataField="PotasiumValue" HeaderText="PotasiumValue"  SortExpression="PotasiumValue"/>
        </Columns>
        </asp:GridView>

    
    </div>
    </form>
</body>
</html>

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try this



Imports System.Data

Partial Public Class sort
    Inherits System.Web.UI.Page

    Public Property TimeSortDirection() As String
        Get
            Return ViewState("Time")
        End Get
        Set(ByVal value As String)
            ViewState("Time") = value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Me.TimeSortDirection = "ASC"
        End If
        
        GridView1.DataSource = GetSortedDate("")
        GridView1.DataBind()

    End Sub

Private Function GetSortedDate(SortExpression As String) As DataView
   Dim ds As New DataSet()
   If IsNothing(Sesstion("MyDate")) Then
      ds = New DataSet()
    ds.ReadXml("\\wintdc02\PUBLIC\TarehouseXML\BSUGBLB017032011111.xml")
      Session("MyData") = ds
   End If
   ds = Session("MyData")
   ds.Tables(0).DefaultView.Sort = SortExpression
   Return ds.Tables(0).DefaultView
End Function
    Protected Sub GridView1_Sorting(ByVal sender As Object, _
                                    ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        If e.SortExpression = "Time" Then
            If Me.TimeSortDirection = "ASC" Then
                e.SortExpression = "Time DESC"
            Else
                e.SortExpression = "Time ASC"
                Me.TimeSortDirection = "ASC"

            End If
            GridView1.DataSource = GetSortedDate(e.SortExpression)
            GridView1.DataBind()
        End If
    End Sub


End Class

Open in new window

Hi

Unfortunately none of these worked, however I did manage to find elsewhere a solution to get this working as per the below code
Public Shared Function GetDataAsDataSet( _
        ByVal dirty1 As String, _
        ByVal dirty2 As String, _
        ByVal clean1 As String, _
        ByVal clean2 As String, _
        ByVal clean3 As String, _
        ByVal clean4 As String, _
        ByVal year As String, _
        ByVal month As String, _
        ByVal day As String) _
        As DataSet

        Dim xmlreader1 As New XmlTextReader("\\pagbssc1web003\XML\" & dirty1 & year & month & day & ".xml")
        Dim xmlreader2 As New XmlTextReader("\\pagbssc1web003\XML\" & dirty2 & year & month & day & ".xml")
        Dim xmlreader3 As New XmlTextReader("\\pagbssc1web003\XML\" & clean1 & year & month & day & ".xml")
        Dim xmlreader4 As New XmlTextReader("\\pagbssc1web003\XML\" & clean2 & year & month & day & ".xml")
        Dim xmlreader5 As New XmlTextReader("\\pagbssc1web003\XML\" & clean3 & year & month & day & ".xml")
        Dim xmlreader6 As New XmlTextReader("\\pagbssc1web003\XML\" & clean4 & year & month & day & ".xml")

        'Although I'm pretty sure you don't need XmlTextReaders - file names would do:
        'Dim fileNameFormat As String = "\\wintdc02\public\TarehouseXML\{0}{1}{2}{3}.xml"
        
        Dim ds1 As New DataSet()

        Try
            ds1.ReadXml(xmlreader1) 'or 'ds1.ReadXml(dirty1FileName)
            Dim ds2 As New DataSet()
            ds2.ReadXml(xmlreader2)
            Dim ds3 As New DataSet()
            ds3.ReadXml(xmlreader3)
            Dim ds4 As New DataSet()
            ds4.ReadXml(xmlreader4)
            Dim ds5 As New DataSet()
            ds5.ReadXml(xmlreader5)
            Dim ds6 As New DataSet()
            ds6.ReadXml(xmlreader6)

            ds1.Merge(ds2)
            ds1.Merge(ds3)
            ds1.Merge(ds4)
            ds1.Merge(ds5)
            ds1.Merge(ds6)
            ds1.WriteXml("\\pagbssc1web003\XML\all.xml", XmlWriteMode.IgnoreSchema)
            xmlreader1.Close()
            xmlreader2.Close()
            xmlreader3.Close()
            xmlreader5.Close()
            xmlreader6.Close()

        Catch ex As Exception

            HttpContext.Current.Response.Write(ex.Message)

        End Try

        Dim ds As New DataSet()


        If File.Exists("\\pagbssc1web003\XML\all.xml") Then
            ds.ReadXml("\\pagbssc1web003\XML\all.xml")
        Else
            HttpContext.Current.Response.Write(" The file you are looking for does not exist or has been moved")
        End If

        Return ds

    End Function

    Public Shared Function GetSortedData( _
    ByVal dirty1 As String, _
    ByVal dirty2 As String, _
    ByVal clean1 As String, _
    ByVal clean2 As String, _
    ByVal clean3 As String, _
    ByVal clean4 As String, _
    ByVal year As String, _
    ByVal month As String, _
    ByVal day As String, _
    ByVal sortExpression As String, _
    ByVal sortDirection As String) As DataView

        Dim ds As DataSet = GetDataAsDataSet(dirty1, dirty2, clean1, clean2, clean3, clean4, year, month, day)
        Dim dv As DataView = ds.Tables(0).DefaultView

        dv.Sort = sortExpression & " " & sortDirection

        Return dv

    End Function

Open in new window

Author

Commented:
.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
That resembles a lot to my suggestion.

Author

Commented:
which did not work for me, its the gridview sort handler which i was after and is what I have posted up.

Sorry, your response was not right.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
So the  GetSortedData function in http:#37149106 is not a sort handler?

Author

Commented:
it didnt work..

what more can i say, your offereings did not work. If you wish you can take this to a site expert\arbritrator, but your response simply did not work.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You could not get it to work.

How are you calling the function that you have posted?

Author

Commented:
This is how im calling it
SelectMethod="GetDataAsDataSet" TypeName="XMLParser_Tarehouse.TarehouseXMLAccess"
just accept that your solution did not work for me and that I was able to find the solution else where.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
>just accept that your solution did not work for me and that I was able to find the solution else where.

I am accepting that despite all my tries, you could not understand what I was saying.

Show us the markup of the grid.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.