»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books
»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects
»Competition
Community
Winners
Prizes
Write For Us
Members
»Other Resources
Links
Dotnet Resources
|
To add sorting to a datagridThis is the code that goes in the html of the datagrid
/////////code behind
Private Sub dgView_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgView.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"
Else
txtSort.Value = e.SortExpression & " ASC"
End If
Else
txtSort.Value = e.SortExpression & " ASC"
End If
'' Bind the newly sorted data to the DataGrid
dgView.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
dgView.DataSource = dv
dgView.DataBind()
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"
bindData()
End If
End Sub
|
|