簡體   English   中英

如何使asp.net gridview能夠排序?

[英]How do I make my asp.net gridview able to sort?

好的,這是我已經工作了幾個小時了的大問題。

我得到了gridview,並且得到的標頭始終保持在頂部,而我使用jQuery作為標頭,而到達頂部始終不會從gridview允許排序的超鏈接中獲取。

我知道可以通過添加以下內容來對gridview進行排序: ORDER BY ColumnName。 但是我不知道如何創建click事件或如何顯示箭頭(如果可能的話),因此您可以查看它是上升還是下降。 另外,如果您單擊它,它將從升/降更改

到目前為止,我的代碼是:

Side.aspx

            <div id="GHead"></div>
            <div style="overflow: auto; height: 100%">
                <asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoGenerateColumns="false" BackColor="White" Width="100%" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateSelectButton="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <Columns>
                        <asp:BoundField HeaderText="ID" DataField="ID" />
                        <asp:BoundField HeaderText="Tildelt" DataField="Tildelt" />
                        <asp:BoundField HeaderText="Firma" DataField="Firma" />
                        <asp:BoundField HeaderText="Kontakt" DataField="Kontakt" />
                        <asp:BoundField HeaderText="Svar" DataField="Svar" />
                        <asp:BoundField HeaderText="Emne" DataField="Emne" />
                        <asp:BoundField HeaderText="Due Date" DataField="DueDate" />
                        <asp:BoundField HeaderText="Prioritet" DataField="Prioritet" />
                        <asp:BoundField HeaderText="Status" DataField="Status" />
                        <asp:BoundField HeaderText="Lukke Dato" DataField="Lukke Dato" />
                    </Columns>
                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                    <RowStyle BackColor="White" ForeColor="#330099" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                    <SortedAscendingCellStyle BackColor="#FEFCEB" />
                    <SortedAscendingHeaderStyle BackColor="#AF0101" />
                    <SortedDescendingCellStyle BackColor="#F6F0C0" />
                    <SortedDescendingHeaderStyle BackColor="#7E0000" />
                </asp:GridView>
            </div>

jQuery代碼

<script src="../Scripts/jquery-1.7.1.js"></script>
<script lang="ja">
    $(document).ready(function () {
        var gridHeader = $('#<%=GridView1.ClientID%>').clone(true); //clone copy of gridview with style
        $(gridHeader).find("tr:gt(0)").remove(); // removes all rows except first row
        $('#<%=GridView1.ClientID%> tr th').each(function (i) {
            //sets width of each th from gridview
            $("th:nth-child("+(i+1)+")",gridHeader).css('width', ($(this).width() + 1).toString() + "px");
        });
        $("#GHead").append(gridHeader);
        $('#GHead').css('position', 'absolute');
        $('#GHead').css('top', $("#<%=GridView1.ClientID%>").offset().top);

    });
</script>

我從互聯網上獲得了所有的jQuery。

這是現在的樣子。 在此處輸入圖片說明

當框架具有標准功能時,請使用AllowSorting做您想要的事情,並且不要使用jQuery之類的第三方工具。

例如:

<asp:GridView ID="SomeGridView" AutoGenerateColumns="False" runat="server" Width="100%" AllowPaging="True"
    PageSize="50" AllowSorting="True" OnPageIndexChanging="SomeGridView_OnPageIndexChanging"
    OnSorting="SomeGridView_OnSorting" OnRowDataBound="SomeGridView_OnRowDataBound">

好的,我找到了一種解決方案,如何使它們可點擊。 因為它們沒有自動生成,所以我需要為每個邊界域放入SortExpression。

我自己沒有發現,我發現這個鏈接他和我一樣有問題,並且有答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM