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

 Delphi Discussion :

tri sur mes colonnes dbgrid avec firebird


Sujet :

Delphi

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    483
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 483
    Points : 128
    Points
    128
    Par défaut tri sur mes colonnes dbgrid avec firebird
    Décidément pas simple de tous changer .J'ai un problème maintenant avec le trie sur les colonnes de mon DBGrid. Habituellement le code fonctionne bien mais la?
    voici mon code
    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
    procedure Tgestion_clients.SMDBGrid1TitleClick(Column: TColumn);
    VAr
    Tri_Ch:String;
    I:integer;
    begin
     
    //vérifier si le tri et descandant ou ascendant
     
    if Tri_dec=True Then
        begin
        Tri_ch:=' ASC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
        Tri_dec:=False;
        end
      ELSE
        begin
        TRi_Ch:=' DESC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending;
        Tri_Dec:=True;
        end;
    DataModule2.ZQuery1.Close;
    DataModule2.ZQuery1.SQL.Clear;
    DataModule2.ZQuery1.SQL.Add('Select * FROM CLIENT ORDER BY '+Column.Field.DisplayName+Tri_ch);
    DataModule2.ZQuery1.open;
     
    For i:=0 to SMDBGrid1.Columns.Count-1 Do begin
      SMDBGrid1.Columns.Items[i].Title.Font.Color:=clblack;
      SMDBGrid1.Columns.Items[i].SortType:=stNone;
      end;
    //coloré le tite de Tri En Rouge
    SMDBGrid1.Columns.Items[column.Index].Title.Font.Color:=clred;
    If Tri_ch=' ASC' then
    SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending
    else
    SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
    end;
    je travail avec les outils Zéos et firebird 2.5 et delphi7.
    Ce code fonctionne trés bien avec mes tables paradox.
    merci pour votre aide

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 045
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 045
    Points : 40 963
    Points
    40 963
    Billets dans le blog
    62
    Par défaut
    pourtant cela fonctionne très bien , voici un code plus complet dans pratiquement le même environnement

    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
     
    // event sur colonne
    procedure TMain.SMDBGrid1TitleClick(Column: TColumn);
    begin
    with SMDBGrid1 do
    begin
    if Column.Field.FieldNo in [4,17,18,12,13] then
     begin
       case Columns[Column.Index].SortType of
         SmdbGrid.stNone : Columns[Column.Index].SortType:=SmdbGrid.stAscending;
         SmdbGrid.stAscending : Columns[Column.Index].SortType:=SmdbGrid.stDescending;
         SmdbGrid.stDescending: Columns[Column.Index].SortType:=SmdbGrid.stNone;
       end;
     end;
    end;
    Tri;
    Trier;
    end;
     
    // Clause order by
    function TMain.Tri: String;
    var i : Word;
    begin
    result:='';
    With SMDBGrid1 do
    for I := 0 to Columns.Count - 1 do
     begin
       if Columns[i].Visible
       AND (Columns[i].Field.FieldNo in [4,17,18,12,13])
       AND (Columns[i].SortType<>SmdbGrid.stNone)
       then begin
         if Length(Result)>0 then Result:=Result+',';
         Result:=Result+Columns[i].FieldName;
         if Columns[i].Field.FieldNo<>17 then
           begin
            if Columns[i].SortType=SmdbGrid.stAscending
             then Result:=Result+' ASC'
             else Result:=Result+' DESC';
           end
         else begin
          if Length(Result)>0 then Result:=Result+',';
            if Columns[i].SortType=SmdbGrid.stAscending
             then Result:=Result+'SAISON,NUMERO,LIGNE ASC'
             else Result:=Result+'SAISON,NUMERO,LIGNE DESC';
         end;
       end;
     end;
    TriBtn.Enabled:=Length(Result)>0;
    end;
     
    // repositionnement après tri
    procedure TMain.Trier;
    var S : String;
        N,L : Integer;
    begin
    ZQ.DisableControls;
    S:=ZQ.FieldByName('SAISON').AsString;
    N:=ZQ.FieldByName('NUMERO').AsInteger;
    L:=ZQ.FieldByName('LIGNE').AsInteger;
    FetchLignes;
    ZQ.Locate('SAISON;NUMERO;LIGNE',varArrayOf([S,N,L]),[]);
    ZQ.EnableControls;
    end;
     
    // Récupération des Enregistrements
    procedure TMain.FetchLignes;
    var lp : TLigneProd;
        us : TUsage;
        sort: string;
    begin
    if LignesProds.TabIndex<0 then exit;
    LP:= TLigneProd(LignesProds.Tabs.Objects[LignesProds.TabIndex]);
    if Usages.TabIndex<0 then exit;
    US:= TUsage(Usages.Tabs.Objects[Usages.TabIndex]);
    ZQ.DisableControls;
    ZQ.Active:=False;
    ZQ.SQL.Clear;
    ZQ.SQL.Add('SELECT SAISON,NUMERO,LIGNE,FOURNCHOISI,');
    ZQ.SQL.Add('LIGNEPROD,USAGE,QTE,NOTE1,NOTE2,REMARQUE,LAST_MODIF,DATE_LIV,FIN_FABS,');
    ZQ.SQL.Add('DATE_DEPART,COLIS,COMPLEMENT,LANCEMENT,ARTICLE,QTELIVRE,QTERELIQUAT FROM LLANCEMENT');
    ZQ.SQL.Add('WHERE LIGNEPROD='+QuotedStr(LP.Code));
    ZQ.SQL.Add('AND USAGE='+QuotedStr(US.Code));
    if not DataModule1.USER.FieldByName('USINE').isnull then
     begin
      ZQ.SQL.Add('AND FOURNCHOISI='+QuotedStr(DataModule1.USER.FieldByName('USINE').asString));
     end;
    sort:=tri;
    if Length(Sort)>0 then ZQ.SQL.Add('ORDER BY '+sort);
    ZQ.Active:=True;
    // FetchTotaux;    calcul totaux "pied de grid"
    ZQ.EnableControls;
    end;
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    483
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 483
    Points : 128
    Points
    128
    Par défaut
    je ne comprend pas un coup le code fonctionne un autre il ne fonctionne pas sans raison .Je trouve tés instable sans explication . Il y aurait il une raison ?
    j'ai remarquer si je place un statubar sur la form cela ne fonctionne plus drole non ?

  4. #4
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 045
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 045
    Points : 40 963
    Points
    40 963
    Billets dans le blog
    62
    Par défaut
    Citation Envoyé par tarmo57 Voir le message
    .Je le trouve très instable sans explication . Il y aurait il une raison ?
    si tu parles de ton code déjà il y aurait peut être un peu de ménage a y faire, en plus je trouve qu'il y a un incohérence (peut être voulue) entre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    if Tri_dec=True Then
        begin
        Tri_ch:=' ASC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    If Tri_ch=' ASC' then
    SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending

    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
    procedure Tgestion_clients.SMDBGrid1TitleClick(Column: TColumn);
    var
    Tri_Ch:String;
    I:integer;
    begin
     
    //vérifier si le tri et descendant ou ascendant
    // ce qui est en rouge est remplacable par 
    if Tri_dec=True Then
        begin
        Tri_ch:=' ASC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
        Tri_dec:=False;
        end
      ELSE
        begin
        TRi_Ch:=' DESC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending;
        Tri_Dec:=True;
        end;
    
    // cette ligne
    Tri_dec:=not Tri_Dec;
    
    // Pour éviter le 'flickering'
    datamodule2.ZQuery.DisableControls;
    DataModule2.ZQuery1.Close;
    DataModule2.ZQuery1.SQL.Clear;
    DataModule2.ZQuery1.SQL.Add('Select * FROM CLIENT ORDER BY');
    if tri_dec then  ZQuery1.SQL.Add(Column.Field.DisplayName+' DESC')
                 else  ZQuery1.SQL.Add(Column.Field.DisplayName+' ASC');
    
    For i:=0 to SMDBGrid1.Columns.Count-1 Do begin
      SMDBGrid1.Columns.Items[i].Title.Font.Color:=clblack;
      SMDBGrid1.Columns.Items[i].SortType:=stNone;
      end;
    
    //colorer le tite de Tri En Rouge
    SMDBGrid1.Columns.Items[column.Index].Title.Font.Color:=clred;
    If Tri_dec then
    SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending
    else
    SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
    
    // Déplacé
    DataModule2.ZQuery1.open;
    DataModule2.ZQuery1.EnableControls;
    end;
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    483
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 483
    Points : 128
    Points
    128
    Par défaut
    Citation Envoyé par SergioMaster Voir le message
    si tu parles de ton code déjà il y aurait peut être un peu de ménage a y faire, en plus je trouve qu'il y a un incohérence (peut être voulue) entre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    if Tri_dec=True Then
        begin
        Tri_ch:=' ASC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    If Tri_ch=' ASC' then
    SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending

    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
    procedure Tgestion_clients.SMDBGrid1TitleClick(Column: TColumn);
    var
    Tri_Ch:String;
    I:integer;
    begin
     
    //vérifier si le tri et descendant ou ascendant
    // ce qui est en rouge est remplacable par 
    if Tri_dec=True Then
        begin
        Tri_ch:=' ASC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
        Tri_dec:=False;
        end
      ELSE
        begin
        TRi_Ch:=' DESC';
        SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending;
        Tri_Dec:=True;
        end;
    
    // cette ligne
    Tri_dec:=not Tri_Dec;
    
    // Pour éviter le 'flickering'
    datamodule2.ZQuery.DisableControls;
    DataModule2.ZQuery1.Close;
    DataModule2.ZQuery1.SQL.Clear;
    DataModule2.ZQuery1.SQL.Add('Select * FROM CLIENT ORDER BY');
    if tri_dec then  ZQuery1.SQL.Add(Column.Field.DisplayName+' DESC')
                 else  ZQuery1.SQL.Add(Column.Field.DisplayName+' ASC');
    
    For i:=0 to SMDBGrid1.Columns.Count-1 Do begin
      SMDBGrid1.Columns.Items[i].Title.Font.Color:=clblack;
      SMDBGrid1.Columns.Items[i].SortType:=stNone;
      end;
    
    //colorer le tite de Tri En Rouge
    SMDBGrid1.Columns.Items[column.Index].Title.Font.Color:=clred;
    If Tri_dec then
    SMDBGrid1.Columns.Items[column.Index].SortType:=stDescending
    else
    SMDBGrid1.Columns.Items[column.Index].SortType:=stAscending;
    
    // Déplacé
    DataModule2.ZQuery1.open;
    DataModule2.ZQuery1.EnableControls;
    end;
    Merci ton code fonctionne bien ,mais la cause de mon problème était , j'avais un autre query active :=true
    et la sa merde

    merci de ton aide

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 20/10/2014, 10h15
  2. Question sur les tableaux avec en-têtes fixes et tri sur les colonnes
    Par lolo5935 dans le forum Général JavaScript
    Réponses: 14
    Dernier message: 29/07/2010, 15h50
  3. Tri sur une colonne d'un champs calculé DBGRID
    Par BuzzLeclaire dans le forum Bases de données
    Réponses: 13
    Dernier message: 18/02/2009, 15h13
  4. [struts]: Pagination et tri sur les colonnes
    Par sleepy2002 dans le forum Struts 1
    Réponses: 3
    Dernier message: 09/07/2007, 15h16
  5. [VB6][ListView] Tri sur chaque colonne
    Par frlap dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 26/05/2004, 11h20

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