.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Gridview sorting in VB.NET

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: DataGrid    URL: http://www.dotnetspark.com  

This sample code snippet shows how to do gridview sorting in VB.net.

Private Sub gvAttendance_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles gvAttendance.SortCommand
Dim arrSort(1) As String

arrSort = txtSort.Value.Split(" "c)

'' If the selected item to sort by equals the previous item sorted then ...
If e.SortExpression = arrSort(0) Then
'' if the current sort type equals ASC then set sort type equal to DESC
If arrSort(1) = "ASC" Then
txtSort.Value = e.SortExpression & " DESC"
txtSort.Value = e.SortExpression & " ASC"
End If
txtSort.Value = e.SortExpression & " ASC"
End If

'' Bind the newly sorted data to the DataGrid
gvAttendance.EditItemIndex = -1
Call bindData()
End Sub

Private Sub bindData()

Dim dt As New DataTable
dt = getfromdatabase()

Dim dv As DataView = New DataView(dt)
dv.Sort = txtSort.Value

gvAttendance.DataSource = dv

If Not dt Is Nothing Then dt.Dispose()
dt = Nothing
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
txtSort.Value = "ID ASC"
End If
End Sub


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend