Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Protected Sub SortRecords(sender As Object, e As GridViewSortEventArgs)

Dim dt As New DataTable(GridView1.DataSource)
Dim SortDir As String = String.Empty
Dim sortExpression As String = e.SortExpression
Dim sortedView As DataView = dt.DefaultView


If sortedView IsNot Nothing Then
If direction = SortDirection.Ascending Then
direction = SortDirection.Descending
SortDir = "Desc"

Else
direction = SortDirection.Ascending

SortDir = "Asc"

End If
sortedView.Sort = e.SortExpression & " " & SortDir 'error occur on this line

GridView1.DataSource = sortedView
GridView1.AllowSorting() = True
GridView1.DataBind()
End If

End Sub



Public Property direction As SortDirection
Get
If ViewState("direction") Is Nothing Then
ViewState("direction") = SortDirection.Ascending
End If
Return DirectCast(ViewState("direction"), SortDirection)
End Get
Set(ByVal Value As SortDirection)
ViewState("direction") = Value
End Set
End Property

What I have tried:

I've tried using other method but the problem still occur
Posted
Updated 20-Apr-16 21:20pm

Start with the debugger: put a breakpoint on the line where the error occurs, and look at exactly what is in both e.SortExpression and SortDir.
Then look at exactly what columns are in your dt - we can't do any of that for you: we don't have access to your data!
If you can't use the debugger for this because it isn't running on your development machine, then log the values to a file, and examine the file after the error occurs.

At a guess, the problem is elsewhere in your code - perhaps the Gridview DataSource isn't set yet - but without clear information we are even more in the dark than you are! :laugh:
 
Share this answer
 
Comments
Member 12474994 21-Apr-16 3:56am    
already do the breakpoint .

sortedView.Sort = e.SortExpression & " " & SortDir 'error occur on this line



for the e.SortExpression..it match the column's name that can't be find which is the error that occured to me right now. I'm totally clueless right now. But stil
Thanks for the respond
OriginalGriff 21-Apr-16 4:06am    
So show us exactly what the strings were, and exactly what the columns were.

Remember that we can't see your screen, access your HDD, or read your mind! :laugh:
Member 12474994 21-Apr-16 4:45am    
I'm sorry .
the error shown is
An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code

Additional information: Cannot find column MBTBoardNo.
Name Value Type
SortDir "Desc" String
sortExpression "MBTBoardNo" String

is this what you needed? I'm sorry it just I'm still new with vb
OriginalGriff 21-Apr-16 5:03am    
Well - we know the column name it's looking for! That's a good start.
Now look at the data it's searching on - dt - and list out it's Columns.
I think you are wrong when you try to setup the sort direction in the the sort expression itself. You should use the SortDirection property of the GridViewSortEventArgs instead:
Replace
VB
sortedView.Sort = e.SortExpression & " " & SortDir

with
VB
e.SortDirection = direction

You can also get rid of your SortDir variable.
 
Share this answer
 
Comments
Member 12474994 21-Apr-16 3:55am    
I've tried your method but there aren't any data shown after I've tried to sort the data. the whole page is blank. Thanks for your respond

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900