C # infinite loop when sorting GridView

I want to sort my GridView in CodeBehind, but my sort method is giving me an infinite loop.

My GridView for testing looks like this:

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

      

I am creating DataSource in Page_Load.

My sorting method:

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

      

I made it generic because I will be using it for another GridView on the same page.

EDIT: I am changing a lot of things and it works now.

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

      

I am using a local variable like:

Finally, at the end of my DataSource () method, I order my DataSource (of type 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();                
}

      

+3


source to share


1 answer


You shouldn't call Sort

this event inside this, as it will actually go through an infinite loop.

What you need to do is handle the sort. When you look at the MSDN sample , you will see that you need to sort the dataset by grid.



If you have DataTable

, for example, you should sort it something like this:

DataTable dt; // define your data table

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

      

0


source







All Articles