Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sorting the Data in mmGridView
Message
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01180309
Message ID:
01180627
Views:
18
Rick,

>I currently have the Header Text so they can click on it.
>That calls the event: grdItems_Sorting.
>I can capture which column was clicked by looking at e.SortExpression.
>I did set up a second function in the business rule to return the data with a different ORDER BY.
>
>But, I can't seem to resort the data without getting new data from the server. I just want to sort the grid.

It looks like you aren't using paging in the GridView, so here are instructions for a non-paged GridView:

  1. Bind the GridView to a DataView rather than a DataTable. The easiest way to do this is to set the BindingSourceMember property of the GridView to the DefaultView of a DataTable. For example, Orders.DefaultView.

    For details on binding to DataViews, see the MM .NET Help topic Data Binding Web Forms Data Grids.

  2. Select the GridView in design mode and set its AllowSorting property to true.

  3. To specify the columns on which you want to allow end users to sort, select the GridView in design mode, click it's smart tag and select Edit Columns... from the shortcut menu which launches the Fields dialog.

    Next, in the Selected fields list select the column you want to be sortable, and in the property list on the right side of the dialog, set the column's SortExpression property to the expression you want to sort on. Typically this is the same as the DataField property.

    Set the SortExpression property on each column you want to be sortable, then click OK to close the Fields dialog. Your GridView should now display the sortable column header text as hyperlinks that can be clicked to change the sort order of the GridView:

  4. Now you need to create an event handler for the GridView's Sorting event. To do this, select the GridView in design mode, then go to the Properties Window and click the Events button (the button with the lightning bolt). Double click the Sorting event to create an event handler.

  5. In the new event handler method, add code that retrieves the DataTable to which the GridView is bound, and sets its associated DefaultView.SortOrder property based on the currently selected column header.

    For example, in C#:
    protected void grdOrders_Sorting(object sender, GridViewSortEventArgs e)
    {
       // Get the sort direction from the event args
       string SortOrder = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
    
       // Get the SortExpression from the event args and use it along with the sort order to
       // set the DefaultView's Sort property
       DataTable dtOrders = (DataTable)Session["dtOrders"];
       dtOrders.DefaultView.Sort = e.SortExpression + " " + SortOrder;
    
       // Save the DataTable back to the session, then rebind the GridView
       Session["dtOrders"] = dtOrders;
       this.grdOrders.DataSource = dtOrders.DefaultView;
       this.grdOrders.DataBind();
    }
    And in VB .NET:
    Protected Sub grdOrders_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdOrders.Sorting
            
         '' Get the sort direction from the event args
         Dim SortOrder As String = IIf(e.SortDirection = SortDirection.Ascending, "ASC", "DESC")
    
         '' Get the SortExpression from the event args and use it along with the sort order to
         '' set the DefaultView's Sort property
         Dim dtOrders As DataTable = CType(Session("dtOrders"), DataTable)
         dtOrders.DefaultView.Sort = e.SortExpression + " " + SortOrder
    
         '' Save the DataTable back to the session, then rebind the GridView
         Session("dtOrders") = dtOrders
         Me.grdOrders.DataSource = dtOrders.DefaultView
         Me.grdOrders.DataBind()
    
    End Sub


    And that should do it!

    Best Regards,
    Kevin McNeish
    Eight-Time .NET MVP
    VFP and iOS Author, Speaker & Trainer
    Oak Leaf Enterprises, Inc.
    Chief Architect, MM Framework
    http://www.oakleafsd.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform