How to Sorting Gridview in Ascending and Descending Order in asp net

How to Sorting Gridview in Ascending and Descending Order in asp net,GridView Sorting Vb.Net,Switch Ascending and descending,DataView and sorting
Share it:

How to Sorting Gridview in Ascending and Descending Order in asp net

GridView sorting is very easy to enable. But only a few functionalities are available out of the box, so still a few things should be configured and coded. 
The first thing to enable sorting is AllowSorting = true. Then SortExpression has to be specified. Normally column name has to be given for the SortExpression. With this the GridView view will be able to sort based on the SortExpression given.

Gridview in Ascending and Descending Order in asp net


Also check:-ASP. Net ListView DataBinding Tutorial

GridView Sorting Vb.Net

After configuring the above when you click on the sort link in the header of the respective column, an exception will be thrown that the Sorting event is not handled. In this event, we need to sort the data using a DataView. DataView has a Sort that we can use to pass the expression.

Switch Ascending and descending

Actually when a GridView is sorted in either ascending or descending order the next expected has to be opposite for the first sorting. If the first sorting is ascending then the second sorting has to be descending.

DataView and sorting

DataView can be obtained from Data Table’s DefaultView. Data View’s Sort property can sort the data based on the given ascending or descending suffix. After this rebind the GridView to sort

Read Also:- how to ping ip address in asp net




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication2.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <asp:GridView runat="server" AutoGenerateColumns="false" AllowSorting="true"
            ID="GridView1" ShowFooter="true" >
        <Columns>
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName"/>
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"  SortExpression="UnitPrice"/>
        </Columns>
        </asp:GridView>
    </form>
</body>
</html>


Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Private Property SortDirection() As String
        Get
            If (ViewState("SortDirection") Is Nothing) Then ViewState("SortDirection") = String.Empty
            Return ViewState("SortDirection").ToString()
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property
    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
        If (Not IsPostBack) Then
            GridView1.DataSource = GetSortableData(String.Empty) 'System.Drawing.FontFamily.Families
            GridView1.DataBind()
        End If
    End Sub
    Private ReadOnly Property ConnectionString() As String
        Get
            Return "Server=.\SQLEXPRESS;Database=NorthWind;Trusted_Connection=True"
        End Get
    End Property
    Private ReadOnly Property Connection() As SqlConnection
        Get
            Dim ConnectionToFetch As New SqlConnection(ConnectionString)
            ConnectionToFetch.Open()
            Return ConnectionToFetch
        End Get
    End Property
    Public Function GetSortableData(ByVal Expression As String) As DataView
        Dim SelectQry = "select * from Products"
        Dim SampleSource As New DataSet
        Dim SortableView As DataView
        Try
            Dim SampleDataAdapter As New SqlDataAdapter(SelectQry, ConnectionString)
            SampleDataAdapter.Fill(SampleSource)
            SortableView = SampleSource.Tables(0).DefaultView
            If (Not String.IsNullOrEmpty(Expression)) Then
                If (SortDirection.ToUpper() = "ASC") Then
                    SortDirection = "DESC"
                Else
                    SortDirection = "ASC"
                End If
                SortableView.Sort = Expression & " " & SortDirection
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return SortableView
    End Function
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        GridView1.DataSource = GetSortableData(e.SortExpression)
        GridView1.DataBind()
    End Sub
End Class
Share it:

aspnet

vbnet

Post A Comment:

0 comments: