분류에서의 행사에서트 ASP.Net 사용 VB

0

질문

나는 데 문제에 대한 정렬트. 나는 전문가가 아니에 vb.net 그러나 나는이 문제를 해결합니다. 내가 원하는 방법을 설명하는 내 데이터에서트.

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 을 얻을 것이다에 대한 모든 데이터에 대한 쿼리트

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" 그리고 나도 몰라 나는 무엇을 해야 합니다. 나는 다음과 같은 이 Articel

asp.net gridview vb.net
2021-11-20 04:46:50
1

최고의 응답

1

론,기본적인 설정할 수 있다:

고 항상 있는지 IsPost 다시 stub 에서 모든 웹 페이지입니다.

그래서 나는 이 태그:

(내가 사용하는 마법사를 만드는 데이터 소스). 나는 다음을 불어 데이터 원본 설정을 삭제 DataSourc1 웹 페이지에서.

그래서 나는 이 태그:

   <div style="width:40%;padding:25px">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table table-hover" AllowSorting="True" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            </Columns>
        </asp:GridView>
        <br />
    </div>

내 로드하는 코드가 이: (참고 어떻게 내 LoadGrid 보기에는"기본"분류

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadGrid
    End If
End Sub

Sub LoadGrid(Optional strSort = "HotelName")

    GridView1.DataSource = MyRst("SELECT * FROM tblHotels Order by " & strSort)
    GridView1.DataBind()

End Sub

내종 이벤트 stub this:

Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

    LoadGrid(e.SortExpression)

End Sub

과 결과는 이것:

enter image description here

지금은 물론 저는 항상 피곤을 입력 할 필요 연결고 코드 레코드 집합을 만드(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

편집:

그래서 추가적인 질문은 무엇에 대해 정렬하 ASC 및 DESC.

음,우리가 추가할 수 있는 경우에 당신은 클릭하여 제목을 다시,우리는 반대로 정렬 할 수 있습니다.

이것이 조금 더 코드 그러나 이제는 일:

Sub LoadGrid(Optional strSort As String = "HotelName",
             Optional SortASC As Boolean = True)

    Dim rstTable As DataTable
    rstTable = MyRst("SELECT * FROM tblHotels")
    rstTable.DefaultView.Sort = strSort & " " & If(SortASC, "ASC", "DESC")
    GridView1.DataSource = rstTable
    GridView1.DataBind()

    ViewState("Sort") = strSort
    ViewState("SortASC") = SortASC
End Sub

Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

    If ViewState("Sort") = e.SortExpression Then
        ViewState("SortASC") = Not ViewState("SortASC")
    Else
        ViewState("SortASC") = True
    End If
    LoadGrid(e.SortExpression, ViewState("SortASC"))

End Sub
2021-11-21 08:56:42

나는 아직 완료되지 않았지만 저는 이것을 이해하는 솔루션입니다.
Julker Nien Akib

@albert-d-kallal 주셔서 감사합 니스 코드 및 나이애가라폴스 폴스뷰을 마무리 내에 작업 시간입니다.
Julker Nien Akib

안녕하세요,동생,내가 한 데 문제입니다. 에서는 이 솔루션은 이것만이 할 Desc 또는 Asc. 이 될 것입니다 무엇 뒤에 논리를 이??
Julker Nien Akib

Ok,는 더 복잡도에 따라서 물건을 변경한 비트입니다. 하지만 내 편집니다-나는 게시할 수 있는 방법을 클릭의 제목을 클릭하면 다시는 그것을 반대로 정렬한 남는 것을 방지합니다.
Albert D. Kallal

당신의 답변에 감사드립니지만 난 그냥 당신이 전에 편집하지만 이미 많은 나에게는 대답했습니다.
Julker Nien Akib

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................