Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序GridView时的C#无限循环_C#_Asp.net_Gridview_Gridview Sorting - Fatal编程技术网

排序GridView时的C#无限循环

排序GridView时的C#无限循环,c#,asp.net,gridview,gridview-sorting,C#,Asp.net,Gridview,Gridview Sorting,我想在CodeBehind中对GridView进行排序,但我的排序方法给了我一个无限循环 我的GridView(用于测试)如下所示: <asp:GridView ID="GVEquipe" OnRowDataBound="GVEquipe_RowDataBound" OnSorting="GridView_Sorting" AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="Employee" runat="s

我想在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>
我将其设置为通用,因为我将在同一页面中的其他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类型)进行了排序:


您不应该从该事件内部调用
Sort
,因为它确实会进入无限循环

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

例如,如果您有一个
DataTable
,您应该对其进行如下排序:

DataTable dt; // define your data table

dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GVEquipe.DataSource = dt;
GVEquipe.DataBind();

您是从内部调用sort?根据排序情况,使用更新的数据源重新绑定网格direction@felix:需要更多帮助吗?有什么问题吗?我在设置数据表时遇到一些困难。我会这样做:“DataTable dt=Session[“TaskTable”]作为DataTable;”并且我在生成数据源的方法中设置会话,如:“Session[“TaskTable”]=GVEquipe.DataSource;”但是当方法退出时,会话丢失:“+base{”不可能的对象支持。\r\nNom de l'objet:“accèsáDataContext après Dispose.”System.InvalidOperationException{System.ObjectDisposedException}“您应该显示整个代码。我想你现在可以访问聊天室了,让我们看看你是否可以把它放在那里。(呃,不,你不能。没有足够的代表聊天……你能用代码发布一个新问题吗?)对不起,你还不能,因为你没有足够的代表。我会发布一个新问题,用你的代码修复这个问题。
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();                
}
DataTable dt; // define your data table

dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GVEquipe.DataSource = dt;
GVEquipe.DataBind();