Link to home
Start Free TrialLog in
Avatar of SimonPrice33
SimonPrice33

asked on

Sort Gridview Data

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

Avatar of strickdd
strickdd
Flag of United States of America image

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

Avatar of SimonPrice33
SimonPrice33

ASKER

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..
See that Other Versions drop down? Use that.


Capture.JPG
Hi,

Im just not understanding the microsoft examples... however correct they maybe the examples are not clear to me...
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!!
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.
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

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

ASKER CERTIFIED SOLUTION
Avatar of SimonPrice33
SimonPrice33

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.
That resembles a lot to my suggestion.
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.
So the  GetSortedData function in http:#37149106 is not a sort handler?
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.
You could not get it to work.

How are you calling the function that you have posted?
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.
>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.