GridView Sorting Vb.Net


This article is written by Pon Saravanan  on 19-Jan-10 Last modified on :05-May-10





ASP.Net GridView Sorting Tutorial

GridView sorting is very easy to enable. But only 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 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 which 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.

Source Code

MarkUp (Aspx)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>Untitled Page</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>

Code Behind(Aspx.Vb)

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










Comments
  • GUEST
    z 2/21/2011 4:25:04 PM

  • GUEST
    excellent 6/28/2011 1:54:15 AM

  • GUEST
    good
    8/22/2011 1:56:43 AM

  • GUEST
    You may as well do it in the SQL it will be quicker! 10/7/2011 12:26:35 AM

  • GUEST
    Very Good!
    Thanks
    11/28/2011 5:29:17 PM

  • GUEST
    thank you :) 12/9/2011 10:44:21 AM

  • GUEST
    you are amazing ,thank you 12/9/2011 10:45:01 AM

  • GUEST
    Very nice article! helped me a ton 4/12/2012 9:02:29 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Gray
Blue
Brown
GraySimple