ARTICLE

How to sort the Columns in GridView using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 28, 2011
In this article we sort the columns when user clicks on Header link of GridView control.
 
Reader Level:

In this article we sort the columns when user clicks on Header. To do that we drag and drop a GridView control on the asp. net form and bind the data with the GridView control. After that we have specified OnSorting event and AllowSorting=true to the GridView.

GridView looks like the below figure1.

GridView control

Figure1

.aspx code

<asp:GridView ID="GridView1" runat="server" OnSorting="SortRecords" AllowSorting="True" CellPadding="4" >

        </asp:GridView>

Now add the following .VB code.

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            BindData()

        End If

 

    End Sub

    Private Sub BindData()

 

        GridView1.DataSource = Me.GetData()

        GridView1.DataBind()

 

    End Sub

    Protected Sub SortRecords(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

        Dim sortExpression As String = e.SortExpression

        Dim direction As String = String.Empty

        If SortDirection = SortDirection.Ascending Then

            SortDirection = SortDirection.Descending

            direction = " DESC"

        Else

            SortDirection = SortDirection.Ascending

            direction = " ASC"

        End If

        Dim table As DataTable = Me.GetData()

        table.DefaultView.Sort = sortExpression & direction

        GridView1.DataSource = table

        GridView1.DataBind()

    End Sub

 

    Public Property SortDirection() As SortDirection

        Get

            If ViewState("SortDirection") Is Nothing Then

                ViewState("SortDirection") = SortDirection.Ascending

            End If

            Return DirectCast(ViewState("SortDirection"), SortDirection)

        End Get

        Set(ByVal value As SortDirection)

            ViewState("SortDirection") = value

        End Set

    End Property

 

    Private Function GetData() As DataTable

        Dim table As New DataTable()

        Dim conn As New SqlConnection("Data Source=.; uid=sa; pwd=Password$2; database=userinfo")

        Dim sql As String = "SELECT * FROM Articletable"

        Dim ad As New SqlDataAdapter(sql, conn)

        ad.Fill(table)

        Return table

    End Function

 

End Class

 

Now run the application.

gv2.gif

Figure2

Now click on the Id, title, Description, Author columns to sort the data in Ascending or descending order.

Suppose we click on the Author column header to sort.

Sorting columns with GridView

Figure3 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor