Home > Software engineering >  Sorting event in GridView in ASP.Net using VB
Sorting event in GridView in ASP.Net using VB

Time:11-20

I am having a problem regarding Sort in Gridview. I am not an expert in vb.net but I have to solve this problem. I want to explain how my data is coming in 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() will load for the query data by going through some steps

//This function is used for some logic

Private Sub loadgrid() Handles B_SEARCH.Click, chkLegacy.CheckedChanged, gvEmployer.PageIndexChanged
        GridDataLoader() 
    End Sub

eEmployer will get all the data for the query for 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

finally, the data is returning into grid view. but my problem is I am not understanding how can I implement the sorting thing. i changed something is view AllowSorting="true" SortExpression="EmployerName" and i don't know what should i do further. I was following this enter image description here

Now of course I always get tired of having to type connection and code to create a record set (DataTable), so I have this global helper routine:

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
  • Related