我想在CodeBehind中排序我的GridView,但我的排序方法給我一個無限循環。C#無限循環時排序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();
}
我使用局部變量如:
最後,在我的數據源()方法的末尾,我命令我的數據源(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();
}
你打電話從內部排序排序? – artm
使用更新的數據源重新綁定網格取決於排序方向 –