首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排序C#无限循环GridView

问排序C#无限循环GridView
EN

Stack Overflow用户
提问于 2015-05-26 12:22:25
回答 1查看 890关注 0票数 3

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

对于测试而言,我的GridView看起来如下:

代码语言:javascript
复制
<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>

​

我在DataSource中生成Page_Load。

我的分类方法是:

代码语言:javascript
复制
    protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gv = (GridView)sender;            
        gv.Sort(e.SortExpression, e.SortDirection);
    }

我使它成为通用的,因为我将把它用于同一页中的其他GridView。

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

代码语言:javascript
复制
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):

代码语言:javascript
复制
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();                
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-26 12:26:47

您不应该从事件内部调用Sort,因为它确实将处于无限循环中。

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

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

代码语言:javascript
复制
DataTable dt; // define your data table

dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GVEquipe.DataSource = dt;
GVEquipe.DataBind();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30458482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档