排序gridview时无法找到列。 E.sortexpression与列名匹配,但仍然显示无法找到列的错误 [英] Column cant be found when sorting gridview. E.sortexpression match the column name but the error of the column cant be find is still shown
问题描述
受保护的Sub SortRecords(发送者为对象,e为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
如果sortedView IsNot Nothing那么
如果direction = SortDirection.Ascending那么
direction = SortDirection.Descending
SortDir =Desc
否则
方向= SortDirection.Ascending
SortDir =Asc
结束如果
sortedView.Sort = e.SortExpression& &此行上出现SortDir'错误
GridView1.DataSource = sortedView
GridView1.AllowSorting()= True
GridView1.DataBind()
结束如果
结束子
< br $>
公共物业方向为SortDirection
获取
如果ViewState(方向 )什么都没有
ViewState(direction)= SortDirection.Ascending
结束如果
返回DirectCast(ViewState(direction) ,SortDirection)
结束获取
设置(ByVal值为SortDirection)
ViewState(direction)= Value
结束套件
结束物业
我尝试过:
我尝试过使用其他方法,但问题仍然存在
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
推荐答案
St使用调试器的艺术:在发生错误的行上放置一个断点,并查看e.SortExpression
和SortDir
。
然后仔细查看dt
中的哪些列 - 我们不能为您做任何事情:我们无法访问您的数据!
如果由于未在开发计算机上运行而无法使用调试器,请将值记录到文件中,然后检查文件发生错误后。
猜测,问题出现在代码的其他地方 - 也许Gridview DataSource尚未设置 - 但没有明确的信息我们甚至在黑暗中比你更多! :laugh:
Start with the debugger: put a breakpoint on the line where the error occurs, and look at exactly what is in bothe.SortExpression
andSortDir
.
Then look at exactly what columns are in yourdt
- 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:
当你尝试在sort表达式中设置排序方向时,我认为你错了。您应该使用GridViewSortEventArgs的SortDirection属性:
替换
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
sortedView.Sort = e.SortExpression & " " & SortDir
with
with
e.SortDirection = direction
你也可以摆脱你的SortDir变量。
You can also get rid of your SortDir variable.
这篇关于排序gridview时无法找到列。 E.sortexpression与列名匹配,但仍然显示无法找到列的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!