Dynamically Sort GridView

Last post 09-11-2007 3:03 PM by ddelella. 2 replies.

Sort Posts:

  • Dynamically Sort GridView

    09-11-2007, 2:12 PM
    • Member
      129 point Member
    • fshuja
    • Member since 06-14-2006, 1:56 PM
    • Posts 50

    hi,

    i have a GridView which is bind to an ObjectDataSource. GridView AllowSorting property is true. how can i sort the data in GridView dynamically. i dont want to change the code/stored procedures in the Object to which it is bound. if i add GridView_onSorting method then it does not called when the page loads first time, it only calls when a user clicks on a link in the header column

     

     

    thnx 

    Filed under:
  • Re: Dynamically Sort GridView

    09-11-2007, 2:48 PM
    Answer
    • All-Star
      54,677 point All-Star
    • DarrellNorton
    • Member since 04-04-2003, 3:49 PM
    • VA, USA
    • Posts 6,578
    • Moderator
      TrustedFriends-MVPs
    Add the same code in the OnSorting method to your Page_Load method.
    Darrell Norton, MVP
    Darrell Norton's Blog


    Please mark this post as answered if it helped you!
  • Re: Dynamically Sort GridView

    09-11-2007, 3:03 PM
    Answer
    • Member
      22 point Member
    • ddelella
    • Member since 08-17-2007, 2:47 PM
    • Cincinnati, OH
    • Posts 69

    Your executed command to your database should be something like the following.  I am not gonna show you how to execute a command to the database cause I figure you got that part taken care of.

    SELECT COLUMN1, COLUMN2 FROM MYTABLE 

    If executed correctly and bound correctly you will need to add the following columns to the aspx DataGrid object 

    <asp:DataGrid ID="myDataGrid" Runat="server" AutoGenerateColumns="False" AllowSorting="True">   
        <Columns>
            <asp:BoundColumn HeaderText="Column 1" DataField="COLUMN1" SortExpression="COLUMN1" ReadOnly="True" />
            <asp:BoundColumn HeaderText="Column 2" DataField="COLUMN2" SortExpression="COLUMN2" ReadOnly="True" />
        </Columns>
    </asp:DataGrid> 

    Next is the sorting event handler.  My example uses a DataTable because thats what I used when I wrote my code...sorry

    Protected Sub Sort(ByVal Source As Object, ByVal e As DataGridSortCommandEventArgs) Handles myDataGrid.SortCommand
        Dim Column As String = myDataTable.DefaultView.Sort.Replace(" asc", "").Replace(" desc", "")
        Dim Order As String = " asc"
        If e.SortExpression = Column Then
            If
    myDataTable.DefaultView.Sort.EndsWith(" asc") Then
                Order = " desc"
                myDataTable.DefaultView.Sort = e.SortExpression + Order
            ElseIf myDataTable.DefaultView.Sort.EndsWith(" desc") Then
                myDataTable.DefaultView.Sort = e.SortExpression + Order
            Else
                myDataTable.DefaultView.Sort = e.SortExpression + Order
            End If
        Else

            myDataTable.DefaultView.Sort = e.SortExpression + Order
        End If

        myDataGrid.EditItemIndex = -1
        BindData()
    End Sub

    I can not recall if you need to store the data source object in the session to avoid the sort order being lost in the post back call but you might if you find the sorting not carrying during pagination.  This is a general way of getting bound columns to sort.  I prefer using a DataTable instead of DataSource but thats me.

Page 1 of 1 (3 items)
Microsoft Communities