c# GridView排序时的无限循环

本文关键字:无限循环 排序 GridView | 更新日期: 2023-09-27 17:50:48

我想在CodeBehind中排序我的GridView,但是我的排序方法给了我一个无限循环。

我的GridView,用于测试,看起来像:
<asp:GridView ID="GVEquipe" OnRowDataBound="GVEquipe_RowDataBound"  OnSorting="GridView_Sorting"    AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="Employee" runat="server">
<Columns>
    <asp:HyperLinkField DataTextField="Employee" DataNavigateUrlFields="Employee" DataNavigateUrlFormatString="~/Profil.aspx?No_Emp={0}" HeaderText="No d'employé" SortExpression="Employee" />
    <asp:BoundField DataField="FirstName" HeaderText="Prénom" SortExpression="FirstName" />
    <asp:BoundField DataField="Name" HeaderText="Nom" SortExpression="Name" />
    <asp:BoundField DataField="Machine" HeaderText="Machine" SortExpression="Machine" />
    <asp:TemplateField HeaderText="Infractions" SortExpression="Alerte">
        <ItemTemplate>
            <asp:ImageButton ID="IBAlerte" runat="server" ImageUrl='<%# Convert.ToDouble(Eval("Alerte")) >= 5d ? "~/Images/alerte3.PNG" : Convert.ToDouble(Eval("Alerte")) < 3d ? "~/Images/alerte0.PNG" : "~/Images/alerte2.PNG" %>' CommandArgument='<%# Bind("Employee") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Événements" >
        <ItemTemplate>
            <asp:ImageButton ID="IBDelai" ImageUrl="~/Images/loupe.png" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

在Page_Load中生成数据源。

我的排序方法是:

    protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gv = (GridView)sender;            
        gv.Sort(e.SortExpression, e.SortDirection);
    }

我让它通用,因为我将使用它在同一页面的其他GridView。

编辑:我改变了很多东西,现在是工作。

protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
    sortDirection = e.SortDirection;
    GridView gv = (GridView)sender;
    if (gv.ID == "GVEquipe")
        equipeColumnToSort = e.SortExpression;
    DataSource();
}

我使用局部变量,如:

最后,在我的DataSource()方法的末尾,我命令我的DataSource(类型为IEnumerable):

if (!String.IsNullOrEmpty(equipeColumnToSort))
{
    switch (equipeColumnToSort)
    {
        case "Employee":
            listEquipes = listEquipes.OrderBy(x => x.Employee);
            break;
        case "FirstName":
            listEquipes = listEquipes.OrderBy(x => x.FirstName);
            break;
        case "Name":
            listEquipes = listEquipes.OrderBy(x => x.Name);
            break;
        case "Machine":
            listEquipes = listEquipes.OrderBy(x => x.Machine);
            break;
        case "Alerte":
            listEquipes = listEquipes.OrderBy(x => x.Alerte);
            break;
    }
    if (sortDirection == SortDirection.Descending)
        listEquipes = listEquipes.Reverse();                
}

c# GridView排序时的无限循环

您不应该在该事件中调用Sort,因为它确实会进入无限循环。

应该做的是处理排序。当您查看MSDN上的示例时,您将看到您必须对网格视图后面的数据集进行排序。

例如,如果你有一个DataTable,你应该像这样排序:

DataTable dt; // define your data table
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GVEquipe.DataSource = dt;
GVEquipe.DataBind();