我在 Gridview 中遇到排序問題。我不是 vb.net 的專家,但我必須解決這個問題。我想解釋一下我的資料是如何進入 GridView 的。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//also there is some logic in there but I think that part of the code will not effect
loadgrid()
End Sub
loadgrid() 將通過一些步驟加載查詢資料
//這個函式用于一些邏輯
Private Sub loadgrid() Handles B_SEARCH.Click, chkLegacy.CheckedChanged, gvEmployer.PageIndexChanged
GridDataLoader()
End Sub
eEmployer 將獲取 gridView 查詢的所有資料
Public Sub GridDataLoader()
//some code was there because of searching
Dim dataTable = Employer.getEmployers(eEmployer, chkLegacy.Checked)
gvEmployer.DataBind()
End Sub
Public Function GetEmployers(ByVal eEmployer As tblEmployer, ByVal All As Boolean, Optional ByVal sortExpression As String = Nothing) As DataTable
Dim query = ""
query =
"select employer.EmployerID as EmployerId,
employer.Employer_Name as EmployerName,
// the query is so large so i delete all for better understanding
on (employer.Modified_by=tum.UserID)
where employer.LegacyID IS NULL and address.ValidityTo is null"
'End If
Dim params = ""
If All = False Then
query = " AND employer.ValidityTo is null"
End If
If (params.Trim() IsNot "") Then
query = query & params
End If
data.setSQLCommand(query, CommandType.Text)
Return data.Filldata
End Function
最后,資料回傳到網格視圖中。但我的問題是我不明白如何實作排序。我改變了一些觀點AllowSorting="true" SortExpression="EmployerName",我不知道我應該怎么做。我正在關注這篇
當然,現在我總是厭倦必須鍵入連接和代碼來創建記錄集(DataTable),所以我有這個全域幫助程式:
Public Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361685.html
下一篇:在對話框拖動上更改游標
