[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

How do I do the Sort in Aesc and Desc order in Web Grid

Dear all experts,

  I'm going to do the following in my asp.net (vg.net) web grid program.  When user first time clicks the header of the column, it will sort it in acesding order, when he clicks second time of the same header, it will sort it in descding order.  My code are as follow:

    Private Sub masterDataGrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles masterDataGrid.SortCommand
        Me.DataView1.Sort = e.SortExpression
       
        Me.masterDataGrid.DataBind()
    End Sub

How I can do it dynamically?

Thanks



0
LegalIT
Asked:
LegalIT
1 Solution
 
nauman_ahmedCommented:
Following can be found on MSDN:

Adding Sorting to a DataGrid Web Server ControlSee Also
Adding DataGrid Web Server Controls to a Web Forms Page | DataGrid Web Server Control | Introduction to the DataGrid Web Server Control | Paging Behavior in DataGrid Web Server Controls
The DataGrid Web server control provides a way for you to add sorting to the grid using these methods:

Default sorting    All columns in the grid can be sorted. The header for each column contains a LinkButton control (hyperlink) that users click to sort by that column.
Custom sorting    You define which columns can be sorted and what type of button in the column heading users click to sort.
In either case, the grid does not sort the rows. Instead, it notifies you of the sort request by raising an event. You then perform the sort in your own code, usually by sorting and then rebinding to the data source.

Specifying Default Sorting
To use default sorting, you set properties of the DataGrid control to enable sorting for all columns. You then create an event handler to perform the sort.

To specify default sorting

In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
In the DataGrid Properties dialog box, click the General tab.
Set the grid's AllowSorting property to true.
At run-time, the headers for all columns are automatically rendered as LinkButton controls. When the user clicks a column head, the name of the bound field for that column is passed as the sort key.

Specifying Custom Sorting
You can define custom sorting by including bound columns and template columns in the grid and writing code in the SortCommand event to sort the data.

To specify custom sorting

In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
In the DataGrid Properties dialog box, click the General tab.
In the Behavior section, select the Allow sorting box.
Switch to the Columns tab.
Add any combination of bound columns and template columns to the DataGrid control. For details, see Adding Bound Columns to a DataGrid Web Server Control and Adding Template Columns to a DataGrid Web Server Control.
To enable sorting for a column, enter a value for the Sort expression for that column. This can be any string value, but is typically the name of the data field to which the column is bound.
Note   Columns without a sort expression will not raise the SortCommand event.
Add an event handler for the SortCommand event and continue as described in the section below, "Performing the Sort." For more information on event handlers, see Creating Event Handlers in Web Forms Pages.
If you include template columns, you can specify what type of button a user clicks in the column heading to raise the SortCommand event.

To specify a button type for sorting in a template column

Select the template column to work with and enter a value for its Sort expression. This can be any string value, but is typically the name of the data field to which the column is bound.
Edit the header template for that column. For details about editing templates, see Creating Web Server Control Templates.
Add a Button, LinkButton, or ImageButton control to the template and set its properties to specify a caption or graphic.
Set the Command property of the button to sort (case-sensitive).
Add an event handler for the SortCommand event and continue as described in the next section, "Performing the Sort." For more information on event handlers, see Creating Event Handlers in Web Forms Pages.
Performing the Sort
When the user clicks the button or hyperlink in the header that has sorting enabled, the SortCommand event for the DataGrid is raised. In the event handler, you sort the data and rebind it to the DataGrid.

To respond to a sort request

Create an event handler for the grid's SortCommand event. In the method:
Get the sort key (the value you set in the Sort expression) from the SortExpression property of the DataGridSortCommandEventArgs argument.
Sort the data.
Rebind the data from the data source.
The following example shows how to sort by handling the SortCommand event. The method gets the sort key value and uses it to set the Sort property of a DataView object. The code then rebinds the grid to the DataView object, which automatically returns the data in sorted order.

' Visual Basic
Private Sub DataGrid1_SortCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
Handles DataGrid1.SortCommand
   DataView1.Sort = e.SortExpression
   DataGrid1.DataBind()
End Sub

// C#
private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
   dataView1.Sort = e.SortExpression;
   DataGrid1.DataBind();
}

HTH, Nauman.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now