我在前面的代码中有一个对象,它绑定在datagrid上。现在,我需要对列进行排序。我在网上搜索以从数据源转换dataTable。但是我收到一个错误消息:“在使用DataView之前必须先设置DataTable”。 有人告诉我该怎么做吗?预先感谢。
有绑定数据网格的代码:
Dim thisOrder as New co.Orders(123)
dgrdOrders.DataSource=thisOrder
dgrdOrders.DataBind()
有我的代码:
Private Sub dgrdOrders_SortCommand(source As Object, e As DataGridSortCommandEventArgs) Handles dgrdOrders.SortCommand
Dim dataTable As DataTable = TryCast(dgrdOrders.DataSource, DataTable)
Dim dv As New DataView(dataTable)
dv.Sort = e.SortExpression
dgrdOrders.DataSource = dv
dgrdOrders.DataBind()
End Sub
答案 0 :(得分:1)
最后,我找到了对其进行排序的方法:
Private Sub dgrdOrders_SortCommand(source As Object, e As DataGridSortCommandEventArgs) Handles dgrdOrders.SortCommand
Dim lstOrders As New List(Of co.Order)
For Each objOrder As co.Order In thisOrder
lstOrders.Add(objOrder)
Next
Select Case e.SortExpression
Case "Type"
lstOrders = lstOrders.OrderBy(Function(x) x.Type).ToList
Case "Region"
lstOrders = lstOrders.OrderBy(Function(x) x.Region).ToList
End Select
dgrdOrders.DataSource = lstOrders
dgrdOrders.DataBind()
End Sub