IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

GridView et sorting [Débutant]


Sujet :

ASP.NET

  1. #1
    Membre chevronné Avatar de Ceddoc
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 493
    Par défaut GridView et sorting
    Bonjour à tous,

    Je dois reprendre un site existant et le modifier, actuellement je travaille sur un gridview qui affiche des éléments. J'aimerais qu'il soit triable en cliquant sur l'entête de la colonne.

    J'ai trouvé qu'il fallait mettre je l'ai fais mais ça n'a rien donné.

    Vu qu'il y a déjà pas mal de choses dans la balise ouvrante du gridview il y a surement quelque chose qui parasite cette propriété.

    Code asp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListeTicket.ascx.cs" Inherits="ListeTicket" %>
    <asp:GridView ID="GV_LISTETIC" runat="server" AllowPaging="False" AutoGenerateColumns="False"
        HorizontalAlign="Center" OnRowDataBound="GridView1_RowDataBound" Width="800px" Height="136px" 
    	OnPageIndexChanging="GV_LISTETIC_PageIndexChanging" OnDataBound="GV_LISTETIC_DataBound" allowsorting="true" 
    	EnableSortingAndPagingCallbacks="True" PageSize="150">
        <Columns>
            <asp:BoundField DataField="Coul_Prio" />
            <asp:BoundField DataField="ID_TIC">
                <ControlStyle Width="40px" />
                <ItemStyle HorizontalAlign="Center" Width="30px" Font-Size="Small" />
                <HeaderStyle BackColor="Silver" HorizontalAlign="Center" />
            </asp:BoundField>
    ...
          </Columns>
        <PagerSettings PageButtonCount="150" />
    </asp:GridView>

    et si besoin, mon ListeTicket.ascx.cs

    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
     
    public partial class ListeTicket : System.Web.UI.UserControl
    {
     
        public string Tech,Etat;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmTicket MonTicket = new cmTicket();
            if (Etat == "O")
            {
                if (Tech == "all")
                {
                    GV_LISTETIC.DataSource = MonTicket.ListeTicketEnCours();
                }
     
            }
            else
            {
                if (Tech == "all")
                {
                    GV_LISTETIC.DataSource = MonTicket.ListeTicket();
                }
     
            }
            GV_LISTETIC.DataBind();
            MonTicket.FermerConnexion();
        }
     
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
     
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                cmTicket monTicket = new cmTicket();
                e.Row.Cells[6].Text = "<b>" + monTicket.NbSuivi(int.Parse(e.Row.Cells[1].Text)) + "</b>";
                if (GV_LISTETIC.Columns[5].Visible)
                {
     
     
                    string Resp = e.Row.Cells[5].Text;
                    if (Resp == "-1")
                    {
                        e.Row.Cells[5].Text = "<b>Non Attribué!</b>";
                    }
                    if (monTicket.GetResp(int.Parse(e.Row.Cells[1].Text)) == "-1")
                    {
     
                        e.Row.Cells[1].Text = "<img src='img/warning.gif' alt='Aucun responsable pour ce ticket!'> " + e.Row.Cells[1].Text;
                    }
     
                }
                else
                {
     
     
                    if (monTicket.GetResp(int.Parse(e.Row.Cells[1].Text)) == "-1")
                    {
     
                        e.Row.Cells[1].Text = "<img src='img/warning.gif' alt='Aucun responsable pour ce ticket!'> " + e.Row.Cells[1].Text;
                    }
     
                }
     
     
     
                string Desc = e.Row.Cells[3].Text;
                if (Desc.Length > 150)
                {
                    e.Row.Cells[3].Text = Desc.Substring(0, 150) + "...";
                }
                e.Row.BackColor = GetColor(e.Row.Cells[0].Text);
                e.Row.Cells[7].BackColor = GetColor(e.Row.Cells[0].Text);
     
            }
     
        }
        protected Color GetColor(string color)
        {
     
            int R = int.Parse(color.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
            int G = int.Parse(color.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
            int B = int.Parse(color.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
            Color Ocolor = Color.FromArgb(R, G, B);
            return Ocolor;
        }
        protected void GV_LISTETIC_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_LISTETIC.PageIndex = e.NewPageIndex;
            GV_LISTETIC.DataBind();
     
        }
        protected void GV_LISTETIC_DataBound(object sender, EventArgs e)
        {
            GV_LISTETIC.Columns[0].Visible = false;
            GV_LISTETIC.Columns[7].ItemStyle.BackColor = Color.Gainsboro;
        }
     
    }

  2. #2
    Membre émérite Avatar de Ramajb
    Homme Profil pro
    ----------------------------
    Inscrit en
    Septembre 2007
    Messages
    476
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ----------------------------

    Informations forums :
    Inscription : Septembre 2007
    Messages : 476
    Par défaut
    Bonjour,

    Ce discussion est fréquemment posée sur ce forum. Une petite recherche et tu trouveras ton bonheur.

  3. #3
    Membre éprouvé
    Inscrit en
    Septembre 2007
    Messages
    1 137
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Septembre 2007
    Messages : 1 137
    Par défaut
    rajoute le Tag SortExpression dans tes balises BoundField et ca devrait marcher

    @Ramajb: ceci est un forum, peut etre bien que cette question a déjà été posé, dans ce cas la, mets le lien sur le post en question.
    La réponse, 'Une petite recherche' ne sert strictement á rien

  4. #4
    Membre émérite Avatar de Ramajb
    Homme Profil pro
    ----------------------------
    Inscrit en
    Septembre 2007
    Messages
    476
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ----------------------------

    Informations forums :
    Inscription : Septembre 2007
    Messages : 476
    Par défaut
    Citation Envoyé par tortuegenie Voir le message
    rajoute le Tag SortExpression dans tes balises BoundField et ca devrait marcher
    C'est pas suffisant, ici il a utilisé la propriété DataSource du gridview pour faire la liaison des données, du coup tu perd toutes les fonctionnalités "standard" du gridview (paging, sorting, mise à jour automatique,etc). La solution c'est d’implémenter manuellement ces fonctionnalités.

    Citation Envoyé par tortuegenie Voir le message
    @Ramajb: ceci est un forum, peut etre bien que cette question a déjà été posé, dans ce cas la, mets le lien sur le post en question.
    La réponse, 'Une petite recherche' ne sert strictement á rien
    Bien sur tu as raison, ça ne sert à rien si on ne sais pas le faire
    L’idée c'est d’éviter les questions répétitives, c''est pourquoi une recherche effectuée avant de poser une question est souhaitée.
    Je vous mets le lien: lien1 lien2
    A+

  5. #5
    Membre éprouvé
    Inscrit en
    Septembre 2007
    Messages
    1 137
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Septembre 2007
    Messages : 1 137
    Par défaut
    Ok, mais ce qui est bizarre c'est que chez moi ca marche en faisant de la meme maniere.

  6. #6
    Membre émérite Avatar de Ramajb
    Homme Profil pro
    ----------------------------
    Inscrit en
    Septembre 2007
    Messages
    476
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : ----------------------------

    Informations forums :
    Inscription : Septembre 2007
    Messages : 476
    Par défaut
    Citation Envoyé par tortuegenie Voir le message
    Ok, mais ce qui est bizarre c'est que chez moi ca marche en faisant de la meme maniere.
    Ah bon , c'est intéressent, j'aimerai voir ton code, pourrais-tu nous le montrer?

  7. #7
    Membre chevronné Avatar de Ceddoc
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 493
    Par défaut
    Quelqu'un pourrait me résumer ce qu'implique d'implémenter tout ça à la main? Les différentes méthodes/étapes à faire.

  8. #8
    Membre Expert
    Avatar de Nicolas Esprit
    Homme Profil pro
    Consultant en technologies
    Inscrit en
    Février 2010
    Messages
    1 467
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Consultant en technologies
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2010
    Messages : 1 467
    Par défaut
    C'est normal qu'il n'y ait pas test du IsPostBack dans ton PageLoad ? Ta GridView est bindée à chaque fois.

  9. #9
    Membre chevronné Avatar de Ceddoc
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 493
    Par défaut
    Alors je suis débutant en ASP.NET (en ASP tout court aussi) et c'est une reprise de code existant donc je ne peux pas expliquer tous les choix qui ont été faits.

    Mais si je comprends bien ta question ça peut être un comportement voulu, car sur cette page on ne fait qu'un affichage après on change obligatoirement de page. Mais j'imagine que si je veux mettre un tri par colonne il faudra que j'implémente un IsPostBack?

  10. #10
    Membre éprouvé
    Avatar de Pelote2012
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2008
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 925
    Billets dans le blog
    2
    Par défaut
    pour le aspx
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
       <asp:GridView ID="grdConstInternes" runat="server" AutoGenerateColumns="False" AllowSorting="True"
                EmptyDataText="Pas de constructeur" EnableSortingAndPagingCallbacks="True" PagerSettings-Visible="true"
                ShowFooter="true">
                <RowStyle CssClass="Ligne" />
                <Columns>
                    <asp:TemplateField ItemStyle-Width="150px" HeaderStyle-VerticalAlign="Top">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnConst" runat="server" CommandName="Trier" CommandArgument="Libelle">Constructeur</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderConst" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="l" runat="server" Text='<%# Bind("Libelle") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtLibConstUpdate" runat="server" Text='<%# Bind("Libelle") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtLibConstInsert" runat="server"></asp:TextBox></FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="60px" HeaderStyle-VerticalAlign="Top" ItemStyle-CssClass="AffEtat">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnActif" runat="server" CommandName="Trier" CommandArgument="EstActif">Actif</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderActif" runat="server"></asp:PlaceHolder>
                            <br />
                            <asp:DropDownList ID="rdlstActif" runat="server" CssClass="DropDownList" AutoPostBack="true"
                                OnSelectedIndexChanged="SelActifChanged">
                                <asp:ListItem Text="Tous" Value="-1" Selected="True"></asp:ListItem>
                                <asp:ListItem Text="Actif" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Inactif" Value="0"></asp:ListItem>
                            </asp:DropDownList>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <div id="ConstAct" runat="server">
                            </div>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkConstActifUpdate" runat="server" />
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:CheckBox ID="chkConstActifInsert" runat="server" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="150px" HeaderStyle-VerticalAlign="Top">
                        <HeaderTemplate>
                            <asp:LinkButton ID="lnkbtnConstV2" runat="server" CommandName="Trier" CommandArgument="ID_EDB_V2">Constructeur V2</asp:LinkButton>
                            <asp:PlaceHolder ID="placeholderConstV2" runat="server"></asp:PlaceHolder>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="l1" runat="server" Text='<%# Bind("ID_EDB_V2") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="lstConstV2Update" runat="server" DataTextField="ID_EDB_V2"
                                DataValueField="ID_EDB_V2">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="lstConstV2Insert" runat="server" DataTextField="ID_EDB_V2"
                                DataValueField="ID_EDB_V2">
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="20px">
                        <ItemTemplate>
                            <asp:ImageButton ID="btnEdit" runat="server" CommandName="Edit" ImageUrl="~/images/edit.png" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:ImageButton ID="btnMaj" runat="server" ImageUrl="~/Images/apply.png" ToolTip="Enregistrer"
                                CommandArgument='<%#  Eval("IDConstructeur") %>' CommandName="Update" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="20px">
                        <ItemTemplate>
                            <asp:ImageButton ID="btnSupp" runat="server" CommandName="Supprimer" ImageUrl="~/images/suppr.png"
                                CommandArgument='<%#  Eval("IDConstructeur") %>' /><cc1:ConfirmButtonExtender ID="CBE"
                                    runat="server" ConfirmText="Voulez vous vraiment supprimer ?" TargetControlID="btnSupp">
                                </cc1:ConfirmButtonExtender>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="~/Images/button_cancel.png"
                                ToolTip="Annuler" CommandName="Cancel" /></EditItemTemplate>
                        <FooterTemplate>
                            <asp:ImageButton ID="btnAdd" runat="server" CommandName="Ajouter" ImageUrl="~/Images/add.png"
                                Style="height: 16px" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle CssClass="Pied" />
                <PagerStyle CssClass="Pied" />
                <SelectedRowStyle BackColor="#E7F5E4" />
                <HeaderStyle CssClass="Entete" />
                <AlternatingRowStyle CssClass="LigneAlt" />
                <PagerTemplate>
                    <asp:LinkButton ID="btnFirst" CommandName="First" runat="server"><<</asp:LinkButton>&nbsp;&nbsp;<asp:LinkButton
                        ID="btnPrevious" CommandName="Previous" runat="server"><</asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label
                            ID="lblPagination" runat="server" Text="Label"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;<asp:LinkButton
                                ID="btnNext" CommandName="Next" runat="server">></asp:LinkButton>&nbsp;&nbsp;<asp:LinkButton
                                    ID="btnLast" CommandName="Last" runat="server">>></asp:LinkButton>&nbsp;&nbsp;&nbsp;
                    &nbsp;<asp:Label ID="lblConstGrd" runat="server" Text="Label"></asp:Label></PagerTemplate>
            </asp:GridView>
    coté VB.net(désolé faudra transcrire)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
     
     Private Sub ChargeDatas()
     
            Dim tmp As Integer
            Dim dtsConst As DataSet = UtilsPS.PS_GetInfosConstructeurs(tmp)
            If dtsConst IsNot Nothing AndAlso dtsConst.Tables.Count > 0 AndAlso dtsConst.Tables(0).DefaultView.Count > 0 Then
                dtvConstruct = dtsConst.Tables(0).DefaultView
                dtvConstructV2 = dtsConst.Tables(1).DefaultView
                dtvGrossiste = dtsConst.Tables(2).DefaultView
            End If
     
            HelperCache.SetObjectToCache(dtvConstructId, dtvConstruct)
            HelperCache.SetObjectToCache(dtvConstructIdV2, dtvConstructV2)
            HelperCache.SetObjectToCache(dtvGrossisteId, dtvGrossiste)
     
        End Sub
     
      ''' <summary>
        ''' Pagination de la liste des constructeurs
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Sub grdConstInternes_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdConstInternes.PageIndexChanging
            Try
     
                BindConst(False)
     
                grdConstInternes.EditIndex = -1
                grdConstInternes.PageIndex = e.NewPageIndex
                grdConstInternes.DataBind()
     
            Catch ex As Exception
                My.Log.WriteException(ex)
            End Try
        End Sub
     
        Private Sub grdConstInternes_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdConstInternes.RowEditing
            grdConstInternes.EditIndex = e.NewEditIndex
            BindConst(False)
        End Sub
     
        Private Sub grdConstInternes_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles grdConstInternes.RowCancelingEdit
            grdConstInternes.EditIndex = -1
            BindConst(False)
        End Sub
     
        Private Sub grdConstInternes_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdConstInternes.RowUpdating
            grdConstInternes.EditIndex = -1
            BindConst(False)
        End Sub
     
        Protected Sub grdConstInternes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdConstInternes.RowDataBound
     
            If e.Row.RowType = DataControlRowType.Header Then
     
                'Mettre flèche dans le sens du tri
                If ViewState("SortExp") IsNot Nothing Then
     
                    Dim ImgSort As Image = New Image()
                    If ViewState("SortOrder").ToString() = "ASC" Then
                        ImgSort.ImageUrl = "~/Images/up.png"
                    Else
                        ImgSort.ImageUrl = "~/Images/down.png"
                    End If
     
                    Dim pH As PlaceHolder = Nothing
                    Select Case ViewState("SortExp")
                        Case "EstActif"
                            pH = e.Row.FindControl("placeholderActif")
                        Case "Libelle"
                            pH = e.Row.FindControl("placeholderConst")
                        Case "ID_EDB_V2"
                            pH = e.Row.FindControl("placeholderConstV2")
     
                    End Select
     
                    If pH IsNot Nothing Then pH.Controls.Add(ImgSort)
     
                End If
     
                Dim lstActif As DropDownList = e.Row.FindControl("rdlstActif")
                If lstActif IsNot Nothing Then lstActif.SelectedValue = FiltreActif
     
            ElseIf e.Row.RowType = DataControlRowType.DataRow Then
     
                Dim Macolumn As DataRowView = e.Row.DataItem()
     
                'Actif
                Dim act As Web.UI.HtmlControls.HtmlGenericControl = CType(e.Row.FindControl("ConstAct"), Web.UI.HtmlControls.HtmlGenericControl)
                If act IsNot Nothing Then
     
                    If Not IsDBNull(Macolumn("EstActif")) AndAlso CBool(Macolumn("EstActif")) Then
                        act.Attributes.Add("class", "actif")
                    Else
                        act.Attributes.Add("class", "inactif")
                    End If
     
                End If
     
                'Mode édition
                Dim chk As CheckBox = CType(e.Row.FindControl("chkConstActifUpdate"), CheckBox)
                If chk IsNot Nothing Then
                    chk.Checked = (Not IsDBNull(Macolumn("EstActif")) AndAlso CBool(Macolumn("EstActif")))
     
                    Dim lst As DropDownList = CType(e.Row.FindControl("lstConstV2Update"), DropDownList)
                    LstConstV2(lst)
                    lst.SelectedValue = Macolumn("ID_EDB_V2").ToString
                End If
     
            ElseIf e.Row.RowType = DataControlRowType.Footer Then
     
                Dim lst As DropDownList = CType(e.Row.FindControl("lstConstV2Insert"), DropDownList)
                LstConstV2(lst)
     
            ElseIf e.Row.RowType = DataControlRowType.Pager Then
                Dim btnFirst, btnPrevious, btnNext, btnLast As LinkButton
     
                btnFirst = e.Row.FindControl("btnFirst")
                If btnFirst IsNot Nothing Then
                    If grdConstInternes.PageIndex = 0 Then
                        btnFirst.Visible = False
                    Else
                        btnFirst.Visible = True
                    End If
                End If
     
                btnPrevious = e.Row.FindControl("btnPrevious")
                If btnPrevious IsNot Nothing Then
                    If grdConstInternes.PageIndex = 0 Then
                        btnPrevious.Visible = False
                    Else
                        btnPrevious.Visible = True
                    End If
                End If
     
                btnNext = e.Row.FindControl("btnNext")
                If btnNext IsNot Nothing Then
                    If grdConstInternes.PageIndex = grdConstInternes.PageCount - 1 Then
                        btnNext.Visible = False
                    Else
                        btnNext.Visible = True
                    End If
                End If
     
                btnLast = e.Row.FindControl("btnLast")
                If btnLast IsNot Nothing Then
                    If grdConstInternes.PageIndex = grdConstInternes.PageCount - 1 Then
                        btnLast.Visible = False
                    Else
                        btnLast.Visible = True
                    End If
                End If
     
                Dim lblPagination, lblConstGrd As Label
                lblPagination = e.Row.FindControl("lblPagination")
                If lblPagination IsNot Nothing Then
                    lblPagination.Text = grdConstInternes.PageIndex + 1 & " sur " & grdConstInternes.PageCount
                End If
     
                lblConstGrd = e.Row.FindControl("lblConstGrd")
                If lblConstGrd IsNot Nothing Then
                    If dtvConstruct.Count > 1 Then
                        lblConstGrd.Text = dtvConstruct.Count & " constructeurs affichés sur " & dtvConstruct.Table.Rows.Count
                    Else
                        lblConstGrd.Text = dtvConstruct.Count & " constructeurs affiché sur " & dtvConstruct.Table.Rows.Count
                    End If
                End If
     
            End If
     
        End Sub
     
        Private Sub LstConstV2(ByRef lst As DropDownList)
            lst.DataSource = dtvConstructV2
            lst.DataBind()
            lst.Items.Insert(0, New ListItem("Aucun", "NULL"))
        End Sub
     
        Protected Sub grdConstInternes_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdConstInternes.RowCommand
            Select Case e.CommandName
     
                Case "Previous"
                    If grdConstInternes.PageIndex > 0 Then
                        BindConst(False)
                        grdConstInternes.EditIndex = -1
                        grdConstInternes.PageIndex = grdConstInternes.PageIndex - 1
                        grdConstInternes.DataBind()
                    End If
                Case "First"
     
                    BindConst(False)
                    grdConstInternes.EditIndex = -1
                    grdConstInternes.PageIndex = 0
                    grdConstInternes.DataBind()
     
                Case "Next"
                    If grdConstInternes.PageIndex < grdConstInternes.PageCount Then
                        BindConst(False)
                        grdConstInternes.EditIndex = -1
                        grdConstInternes.PageIndex = grdConstInternes.PageIndex + 1
                        grdConstInternes.DataBind()
                    End If
                Case "Last"
                    BindConst(False)
                    grdConstInternes.EditIndex = -1
                    grdConstInternes.PageIndex = grdConstInternes.PageCount
                    grdConstInternes.DataBind()
     
                Case "Trier"
     
                    grdConstInternes.EditIndex = -1
                    If ViewState("SortExp") Is Nothing Then
     
                        ViewState("SortExp") = e.CommandArgument.ToString()
                        ViewState("SortOrder") = "ASC"
     
                    Else
     
                        If ViewState("SortExp").ToString() = e.CommandArgument.ToString() Then
     
                            If ViewState("SortOrder").ToString() = "ASC" Then
                                ViewState("SortOrder") = "DESC"
                            Else
                                ViewState("SortOrder") = "ASC"
                            End If
     
                        Else
     
                            ViewState("SortOrder") = "ASC"
                            ViewState("SortExp") = e.CommandArgument.ToString()
     
                        End If
     
                    End If
     
                    BindConst()
     
                Case "Ajouter"
     
                    Dim txtLibConst As TextBox = grdConstInternes.FooterRow.FindControl("txtLibConstInsert")
                    Dim chkActif As CheckBox = grdConstInternes.FooterRow.FindControl("chkConstActifInsert")
                    Dim lstConstV2 As DropDownList = grdConstInternes.FooterRow.FindControl("lstConstV2Insert")
     
                    If txtLibConst IsNot Nothing AndAlso chkActif IsNot Nothing AndAlso lstConstV2 IsNot Nothing Then UpdateConstructeur(-1, txtLibConst.Text.Trim, chkActif.Checked, lstConstV2.SelectedValue, True)
     
                Case "Supprimer"
                    Constructeur.Delete(CInt(e.CommandArgument))
                    ChargeDatas()
                    BindConst()
     
                Case "Update"
     
                    Dim txtLibConst As TextBox = e.CommandSource.FindControl("txtLibConstUpdate")
                    Dim chkActif As CheckBox = e.CommandSource.FindControl("chkConstActifUpdate")
                    Dim lstConstV2 As DropDownList = e.CommandSource.FindControl("lstConstV2Update")
     
                    If txtLibConst IsNot Nothing AndAlso chkActif IsNot Nothing AndAlso lstConstV2 IsNot Nothing Then UpdateConstructeur(CInt(e.CommandArgument), txtLibConst.Text.Trim, chkActif.Checked, lstConstV2.SelectedValue, False)
     
            End Select
        End Sub
     
     Protected Sub SelActifChanged(ByVal sender As Object, ByVal e As System.EventArgs)
     
            FiltreActif = sender.selectedValue
            HelperCache.SetObjectToCache(FiltreActifId, FiltreActif)
            grdConstInternes.EditIndex = -1
            BindConst()
     
        End Sub
     
        Private Sub BindConst(Optional ByVal AvecRazPage As Boolean = True)
            grdConstInternes.EnableViewState = True
     
            Dim dtvTmp As DataView = dtvConstruct.Table.DataSet.Copy().Tables(0).DefaultView
     
            If dtvTmp IsNot Nothing Then
                'Gestion des filtres
                Dim strFiltre As String = Nothing
                If FiltreActif <> Nothing AndAlso FiltreActif <> "-1" Then
                    strFiltre = "EstActif = " & FiltreActif
                End If
                dtvTmp.RowFilter = strFiltre
     
                'Gestion du tri
                If ViewState("SortExp") IsNot Nothing Then
                    dtvTmp.Sort = ViewState("SortExp").ToString() & " " & ViewState("SortOrder").ToString()
                Else
                    dtvTmp.Sort = ""
                End If
     
            End If
     
            If dtvTmp Is Nothing OrElse dtvTmp.Count > 0 Then
                grdConstInternes.DataSource = dtvTmp
     
                If AvecRazPage Then
                    grdConstInternes.AllowPaging = True
                    grdConstInternes.PageSize = 23
                    grdConstInternes.PageIndex = 0
                    grdConstInternes.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast
     
                End If
                grdConstInternes.DataBind()
     
            Else
                'Add a blank row to the dataset
                dtvTmp.AddNew()
     
                'Bind the DataSet to the GridView
                grdConstInternes.DataSource = dtvTmp
                grdConstInternes.DataBind()
     
                'Get the number of columns to know what the Column Span should be
                Dim columnCount As Integer = grdConstInternes.Rows(0).Cells.Count
                'Call the clear method to clear out any controls that you use in the columns.  I use a dropdown list in one of the column so this was necessary.
                grdConstInternes.Rows(0).Cells.Clear()
                grdConstInternes.Rows(0).Cells.Add(New TableCell)
                grdConstInternes.Rows(0).Cells(0).ColumnSpan = columnCount
                grdConstInternes.Rows(0).Cells(0).Text = "Pas de constructeur"
     
            End If
     
        End Sub
    Normalement j'ai mis tous les bouts, au moins ça te donnera une idée, l'exemple est assez complet normalement, car il y a des tri et des filtre

    le méthode HelperCache.SetObjectToCache met en cache et je récupère avec un getcache dans le Load

  11. #11
    Membre chevronné Avatar de Ceddoc
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 493
    Par défaut
    Grâce aux éléments de réponse qui m'ont été donnés ici je suis parvenu à implémenter une gridview qui se charge et se trie comme il faut cependant je suis bloqué pour reproduire une fonctionnalité qui avec l'ancien mode de fonctionnement marchait très bien.

    En fait, j'ai une de mes colonnes dont je me sers uniquement pour lire le code couleur à affecter à la ligne, une fois la table entièrement charger cette colonne est cachée.

    La méthode qui cachait cette colonne était anciennement appelée comme ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    OnDataBound="GV_LISTETIC_DataBound"
    Seulement maintenant si j'essaie de faire ça, j'ai l'impression que la colonne est cachée avant que toutes les couleurs soit lues et donc je me mange une exception.

    Vous avez des suggestions? Comment faire pour que cette méthode soit appelée uniquement si toute la table a été chargée? d'autres idées pour cette histoire de couleur? J'ai l'impression que celui qui avait fait ça s'était compliqué la vie mais maintenant je ne vois pas trop comment faire sans multiplier les accès bdd

    (pour info j'utilise maintenant une datasource + sql plus conventionelle.)

  12. #12
    Membre Expert
    Avatar de Nicolas Esprit
    Homme Profil pro
    Consultant en technologies
    Inscrit en
    Février 2010
    Messages
    1 467
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Consultant en technologies
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2010
    Messages : 1 467
    Par défaut
    Citation Envoyé par Ceddoc Voir le message
    Seulement maintenant si j'essaie de faire ça, j'ai l'impression que la colonne est cachée avant que toutes les couleurs soit lues et donc je me mange une exception.
    Quelle est l'exception exactement, et sur quelle ligne ? As-tu testé en mode débug.

    Après il est sûr que le code a un historique et que sa maintenance complique les choses. Mais à moins d'avoir le temps de tout revoir et refactoriser... il faut faire avec (c'est dingue, c'est toujours comme ça

  13. #13
    Membre chevronné Avatar de Ceddoc
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 493
    Par défaut
    J'ai réussi à me débrouiller en cachant chaque cellule après que la ligne ai été traitée au lieu d'essayer de cacher la colonne entière a la fin.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Gridview et Sorting
    Par mimosa803 dans le forum ASP.NET
    Réponses: 4
    Dernier message: 10/05/2008, 09h24
  2. sorting /paging /select gridview
    Par redev dans le forum ASP.NET
    Réponses: 1
    Dernier message: 04/03/2008, 17h56
  3. Sorting d'un GridView
    Par Poussy-Puce dans le forum ASP.NET
    Réponses: 1
    Dernier message: 17/10/2007, 10h55
  4. Quel est le sender lors d'un sorting de gridview
    Par FamiDoo dans le forum ASP.NET
    Réponses: 4
    Dernier message: 28/08/2007, 20h41
  5. [C#][2.0]Sort dans un GridView
    Par Troopers dans le forum ASP.NET
    Réponses: 4
    Dernier message: 24/04/2007, 19h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo