简体   繁体   English

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

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

Okay so here is the big question that I have been working on for hours now. 好的,这是我已经工作了几个小时了的大问题。

I got my gridview and I got a header that will always stay at top that I use jQuery for the header, I got at the top always wont get the hyperlinks from gridview allow sorting. 我得到了gridview,并且得到的标头始终保持在顶部,而我使用jQuery作为标头,而到达顶部始终不会从gridview允许排序的超链接中获取。

I know that I can sort the gridview by adding: ORDER BY ColumnName. 我知道可以通过添加以下内容来对gridview进行排序: ORDER BY ColumnName。 But I don't know how to create the click event or how to display an arrow (if possible) so you can see if it's in ascending or descending. 但是我不知道如何创建click事件或如何显示箭头(如果可能的话),因此您可以查看它是上升还是下降。 Also if you click it it will change from ascending/descending 另外,如果您单击它,它将从升/降更改

My code is so far: 到目前为止,我的代码是:

Side.aspx 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 code 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>

I got all the jQuery from the internet. 我从互联网上获得了所有的jQuery。

Here is what it looks like now. 这是现在的样子。 在此处输入图片说明

Use AllowSorting proprty to do what do you want and do not use third-party tools like jQuery when framework has standard functionality to do it. 当框架具有标准功能时,请使用AllowSorting做您想要的事情,并且不要使用jQuery之类的第三方工具。

For example: 例如:

<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">

Okay I found a solution how to make them clickable. 好的,我找到了一种解决方案,如何使它们可点击。 sinse they arent automaticly generated I needed to put in SortExpression for each boundfield. 因为它们没有自动生成,所以我需要为每个边界域放入SortExpression。

Did not find it out myself I found this Link where he had a problem just like me and with a answer. 我自己没有发现,我发现这个链接他和我一样有问题,并且有答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM