Factoriser code de tri gridview
Bonjour,
j'ai un gridview bindé à une liste:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
<asp:GridView ID="GridViewGS" runat="server" BackColor="White" BorderColor="Black"
AutoGenerateColumns="False" AllowSorting="True" BorderStyle="None" BorderWidth="1px"
CellPadding="3" OnSorting="GridViewGS_Sorting">
<Columns>
<asp:BoundField DataField="Classement" HeaderText="Classement ▼" SortExpression="Classement">
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Nom ▼" SortExpression="Nom">
<ItemTemplate>
<asp:Image ID="ImgMVP" runat="server" ImageUrl="~/Images/MVP.png" Visible='<%# Eval("EstMVP")%>' />
<asp:Label ID="LblNom" runat="server" Text='<%# Bind("Nom")%>' ForeColor='<%# Eval("CouleurDefenseur") %>' />
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Guilde ▼" SortExpression="NomGuilde">
<ItemTemplate>
<asp:Image ID="ImgGuilde" runat="server" ImageUrl="~/Images/Couronne.png" Visible='<%# Eval("ALaCouronne") %>' />
<asp:Label ID="LblGuilde" runat="server" Text='<%# Bind("NomGuilde")%>' />
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Points" HeaderText="Points ▼" SortExpression="Points">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="NbFrags" HeaderText="Frags ▼" SortExpression="NbFrags">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Victimes">
<ItemTemplate>
<asp:Label ID="LblVictimes" runat="server" Text='<%# Eval("HtmlVictimes") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bourreaux">
<ItemTemplate>
<asp:Label ID="LblBourreaux" runat="server" Text='<%# Eval("HtmlBourreaux") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView> |
dont j'ai implémenté une méthode de tri
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
protected void GridViewGS_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
List<Joueur> joueurs = ViewState["joueurs"] as List<Joueur>;
if (joueurs == null)
return;
string sortExpression = e.SortExpression;
string ancienTri = this.SortExpression;
//si tri sur la même colonne
if (this.SortExpression == e.SortExpression)
{
this.SortDirection = this.SortDirection == SortDirection.Ascending ?
SortDirection.Descending : SortDirection.Ascending;
}
else //tri sur une nouvelle colonne: mettre Ascending
{
this.SortExpression = e.SortExpression;
this.SortDirection = SortDirection.Ascending;
}
//int cell = 0;
//if (GridViewGS.Rows.Count > 0 && GridViewGS.Rows[0].Cells.Count > 0)
//{
// int.TryParse(GridViewGS.Rows[0].Cells[0].Text, out cell);
//}
switch (sortExpression)
{
case "Nom": if (SortDirection == SortDirection.Ascending)
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom);
else
GridViewGS.DataSource = joueurs.OrderByDescending(j => j.Nom);
break;
case "Classement": if (!EstTrieeParClassement())
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderBy(j => j.Classement);
else
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderByDescending(j => j.Classement);
break;
case "NomGuilde": if (SortDirection == SortDirection.Ascending)
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderBy(j => j.NomGuilde);
else
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderByDescending(j => j.NomGuilde);
break;
case "Points": if (SortDirection == SortDirection.Ascending)
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderBy(j => j.Points);
else
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderByDescending(j => j.Points);
break;
case "NbFrags": if (SortDirection == SortDirection.Ascending)
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderBy(j => j.NbFrags);
else
GridViewGS.DataSource = joueurs.OrderBy(j => j.Nom).OrderByDescending(j => j.NbFrags);
break;
default:
break;
}
GridViewGS.DataBind();
}
catch (Exception ex)
{
LblErreur.Text = ex.Message;
}
}
private bool EstTrieeParClassement()
{
if (GridViewGS.Rows.Count > 1 && GridViewGS.Rows[0].Cells.Count > 1)
{
for (int i = 1; i < GridViewGS.Rows.Count; i++)
{
int cellActuel = int.Parse(GridViewGS.Rows[i].Cells[0].Text);
int cellPrecedente = int.Parse(GridViewGS.Rows[i - 1].Cells[0].Text);
if (cellActuel < cellPrecedente)
{
return false;
}
else if (cellActuel == cellPrecedente)
{
string nomActuel = ((Label)GridViewGS.Rows[i].FindControl("LblNom")).Text;
string nomprecedent = ((Label)GridViewGS.Rows[i - 1].FindControl("LblNom")).Text;
int t = nomActuel.CompareTo(nomprecedent);
if (nomActuel.CompareTo(nomprecedent) < 0)
{
return false;
}
}
}
}
return true;
} |
je cherche à factoriser ce code, je pense qu'il y a moyen de faire une requête linq générique, si quelqu'un à une idée la dessus.
Sinon j'étais partie sur une méthode générique similaire à EstTrieeParClassement qui se base sur le nom de la colonne mais ça devient vite trop lourd vu les multiples possibilités (colonne int,string, custom ...)