C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Two ways sorting in a datagrid


Posted Date: 04 Jun 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: ManojRajanMember Level: Gold    
Rating: 1 out of 5Points: 7



Two ways sorting in datagrid

This method does not read data from the database to sort and is advised for small datasets only. The code given below assumes that a dataset (DataSetVar) populated with the data is available.

Put this code in the SortCommand event of the datagrid.


Dim dg As DataGrid = source
Dim StrSort = dg.Attributes("SortExpression")
Dim StrASC = dg.Attributes("SortASC")
dg.Attributes("SortExpression") = e.SortExpression
dg.Attributes("SortASC") = "yes"

If e.SortExpression = StrSort Then
If StrASC = "yes" Then
dg.Attributes("SortASC") = "no"
Else
dg.Attributes("SortASC") = "yes"
End If
End If

‘DataSetVar is the dataset variable which is populated with some data
Dim dt As DataTable = DataSetVar.Tables(0)
Dim dv As DataView = New DataView(dt)
dv.Sort = dg.Attributes("SortExpression")

If dg.Attributes("SortASC") = "no" Then
dv.Sort &= " DESC"
End If
dg.CurrentPageIndex = 0
dg.DataSource = dv
dg.DataBind()



If you want the sorting to persist even in paging use this code in the Page index changed event of the datagrid


Dim dg As DataGrid = source
Dim dt As DataTable = Datasetvar.Tables(0)
Dim dv As DataView = New DataView(dt)
dg.CurrentPageIndex = e.NewPageIndex
dv.Sort = dg.Attributes("SortExpression")

If dg.Attributes("SortASC") = "no" Then
dv.Sort &= " DESC"
End If
dg.DataSource = dv
dg.DataBind()





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Regular Expressions for Beginners
Previous Resource: Change the datagrid footer text
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use