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

Composants VCL Delphi Discussion :

Sélection et désélection de lignes dans un stringgrid


Sujet :

Composants VCL Delphi

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut Sélection et désélection de lignes dans un stringgrid
    Bonjour,

    Avec un bouton je souhaite sélectionner ou désélectionner toutes les ligne d'un stringgrid.

    Cela marche pour la sélection, mais pas pour la désélection.

    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
     
     
    procedure TFlistfact.Button5Click(Sender: TObject);
     
    var
      i:integer;
     
     
    begin
     
    if button5.Caption='&Tout Sélectionner' then
    begin
     
    for i:=1 to stringgrid1.RowCount-1 do
    begin
    rowsselected.Add(i);
    stringgrid1.Invalidate;
    button5.Caption:='&Tout Désélectionner';
    end;
    end
    else
     
    begin
    for i:=1 to stringgrid1.RowCount-1 do
    begin
    rowsselected.Delete(i);
    stringgrid1.Invalidate;
     
    button5.Caption:='&Tout Sélectionner';
    end;
     
     
     
     
    end;
    Merci d'avance pour votre aide et bonnes fêtes.

  2. #2
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Bonjour et bonnes fêtes à toi aussi,

    essaye ceci :

    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
    Procedure TForm1.Button5Click(Sender: TObject);
    var
      myRect : TGridRect ;
    begin
       if (button5.Caption='&Tout Sélectionner') then
          begin
               myRect.Left := 1 ;
               myRect.Top  := 1 ;
               myRect.Right := stringgrid1.ColCount ;
               myRect.Bottom := Stringgrid1.RowCount ;
               StringGrid1.Selection := myRect ;
               Button5.Caption:='&Tout Désélectionner';
          end
       else
          begin
               myRect.Left := -1 ;
               myRect.Top  := -1 ;
               myRect.Right := -1 ;
               myRect.Bottom := -1 ;
               StringGrid1.Selection := myRect ;
               Button5.Caption:='&Tout Sélectionner';
          end;
    End ;
    A+

    Charly

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Bonjour,

    Merci pour ton code, cela marche mais le problème c'est que la sélection n'est pas maintenue si je click dans le stringgrid alors que je veux que ma déselection se fasse uniquement par mon bouton car j'ai un autre bouton qui doit déselectionner ligne par ligne.

    Merci d'avance.

  4. #4
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    tu peux mettre la propriété Enabled à False.

    Alors tu ne pourras plus éditer la grille, mais tu peux ajouter un bouton "Editer" qui bascule cette propriété à True ?

    A+

    Charly

  5. #5
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Sinon, tu mets gère toi même un tableau de booleen ISElect pour indiquer la sélection ou non de la ligne i et tu dessines la grille avec DrawCell :

    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
    procedure TForm1.Button2Click(Sender: TObject);
    var
      i : Integer ;
    begin
       if (button2.Caption='&Tout Sélectionner') then
          begin
             For i := 1 To StringGrid1.RowCount-1 Do
                ISelect[i] := True ;
             StringGrid1.Invalidate ;
             Button2.Caption:='&Tout Désélectionner';
           end
       else
          begin
             For i := 1 To StringGrid1.RowCount-1 Do
                ISelect[i] := False ;
             StringGrid1.Invalidate ;
             Button2.Caption:='&Tout Sélectionner';
          end;
    End ;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    Begin  
      With Sender As TStringGrid Do With Canvas Do
      Begin
        { sélection de la couleur de fond }
        If (ACol > 0 ) Then
        If ISelect[ARow] Then
           Begin
              Brush.Color := clBlue ;
              Font.Color := clWhite ;
           End
        Else
           Begin
              Brush.Color := clBtnFace ;
              Font.Color :=  ClBlack ;
           End ;   
        { Dessin du fond }
        FillRect(Rect);
        { Dessin du texte }
        TextOut(Rect.Left,Rect.Top,Cells[ACol,ARow]);
      End;
    end;
    Charly

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Bonjour Charly,

    Merci pour ton code mais Iselect n'est pas déclaré. Je ne sais pas en quoi je dois le déclarer.

    Merci d'avance.

  7. #7
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    par exemple comme ceci (mais cela dépend du nombre maxi de lignes de ton TStringGrid) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    var
      Form1: TForm1;
      ISelect : Array[0.. 100] of Boolean ;
    ISelect doit être visible dans toute la fiche

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Merci beaucoup le code marche mais ne cohabite pas du tout avec le reste de mon code. En plus avec Array je suis coincé car je ne connais pas le nombre de ligne de mon stringgrid à l'avance et il est variable.

    En fait j'ai un bouton pour "Tout sélectionner/désélectionner" et un bouton pour "sélectionner/déselectioner" les lignes que j'ai sélectionnées au préalable par un double click avec la souris.

    J'ai mis en place un code qui est boiteux par moment, j'arrive à faire fonctionner mon bouton "Tout sélectionner/Désélectionner" si ce n'est que lorsque je sélectionne ou désélectionne ligne par ligne, le bouton "Tout désélectionner" ne déselectionne pas les dernières lignes de mon stringgrid le nombre correspondant aux nombres de lignes que j'ai déselectionné avant.

    Voici le 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
    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
     
     
    procedure TFlistfact.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
     
    begin
     
     
     
    with stringgrid1 do
    begin
    if gdselected in state then
    begin
     
    stringgrid1.Canvas.Brush.Color:=clactivecaption;
    end
    else
    if gdfixed in state then
    stringgrid1.Canvas.Brush.Color:=clbtnface
    else
    begin
    if (arow div 2)=(arow/2) then
    stringgrid1.Canvas.Brush.Color:=clwindow
    else
    stringgrid1.Canvas.Brush.Color:=clyellow;
     
     
     
    end;
     
    stringgrid1.Canvas.TextRect(rect,rect.Left,rect.Top,stringgrid1.Cells[acol,arow]);
     
    if gdfocused in state then
    stringgrid1.Canvas.DrawFocusRect(rect);
    end;
     
     
     
     With Sender As TStringGrid Do With Canvas Do
      Begin
     
     
        //Dessin du fond
        FillRect(Rect);
     
        //Sélection de la couleur de texte
        If gdSelected In State Then
          SetTextColor(Canvas.Handle,clwhite)
        Else SetTextColor(Canvas.Handle,clblack);
     
        //Dessin du texte en utilisant la fonction API
     
        If Arow>0 Then
    Begin
      if ACol in [3..8] then
        DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,
                  DT_RIGHT or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE  )
      else
        DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,
                 DT_center or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE  );
    End
    Else
      DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]),-1, Rect ,
                  DT_center or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE  );
     
      End;
     
     
     
    if RowsSelected.IndexOf(ARow)>-1 then
      begin
        StringGrid1.Canvas.Brush.Color:=clRed;
        stringgrid1.canvas.fillrect(rect);
        stringgrid1.canvas.TextOut(Rect.Left,Rect.Top,stringgrid1.Cells[ACol,ARow]);
      end;
     
    end;
     
     
    procedure TFlistfact.Button3Click(Sender: TObject); //Cest mon bouton pour sélectionner ou désélectionner ligne par ligne
    Var
    index:integer;
    Begin
    if (stringgrid1.Selection.Left>=stringgrid1.FixedCols) and (stringgrid1.Selection.Right>(stringgrid1.ColCount-1)) then
    begin
    if stringgrid1.Row > 0 then
    begin
     
     
     
      if LastRowClicked=-1 then Exit;
      index:=RowsSelected.IndexOf(LastRowClicked);
      if index>-1
      then RowsSelected.Delete(index)
      else RowsSelected.Add(LastRowClicked);
      StringGrid1.Invalidate;
      stringgrid1.col:=1;
      try
      stringgrid1.Row:=stringgrid1.Row+1;
      except
      stringgrid1.row:=stringgrid1.row-1;
      end;
     
     
    end;
    end;
     
     
     
    end;
     
     
    procedure TFlistfact.Button5Click(Sender: TObject);
     
    var
      i:integer;
     
     
    begin
     
    if button5.Caption='&Tout Sélectionner' then
    begin
     
    for i:=1 to stringgrid1.RowCount-1 do
     
    begin
    rowsselected.Add(i);
    stringgrid1.Invalidate;
    button5.Caption:='&Tout Désélectionner';
    end;
    end
    else
     
    begin
     
     
    for i:=stringgrid1.RowCount-2 downto 0 do //Alors ici je ne comprends pas pourquoi cela marche que comme cela ??????
    begin
     
    rowsselected.Delete(i);
    stringgrid1.Invalidate;
     
    button5.Caption:='&Tout Sélectionner';
    end;
     
     
     
     
    end;
     
     
    end;
    Je pense que j'ai du mettre des incohérences dans mon code.

    Merci encore pour ton aide.

  9. #9
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Pour le tableau de Booleen ISelect, tu peux utiliser un tableau dynamique. Ains tu pourras augmenter sa taille comme tu veux

  10. #10
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Chez moi ton code ne compile pas (sous D7) car rowsselected est inconnu.

    Je pense que tu devrais quand même gérer les sélections et déselections par un tableau de booléens, car cela me semble être la méthode la plus simple.

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Pour ce qui manque :

    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
     
     
    public
        { Déclarations publiques }
        RowsSelected:TIntegerList;
        LastRowClicked:integer;
     
     
    end;
     
    var
      Flistfact: TFlistfact;
      Ligne:longint=-1;
     
     
    implementation
     
    uses shellapi,Strutils,chemin, Unit2, UnitIntro,db, UnitListeCourriers,
      Rglt;
     
    {$R *.dfm}
     
    procedure TFlistfact.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
            select:tgridrect;
            c,r:longint;
     
    begin
    stringgrid1.MouseToCell(x,y,c,r);
     
     
     
    if ssdouble in shift then with stringgrid1 do if (c=0) and (r>=fixedrows) then
    begin
     
            perform(wm_killfocus,0,0);
            select.Top:=r;
            select.Bottom:=r;
            select.Left:=fixedcols;
            select.Right:=colcount;
            selection:=select;
     
     
     
    end;
     
    end;
     
    procedure TFlistfact.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    begin
    LastRowClicked:=ARow;
    end;

    Je vais regarder pour le tableau dynamique. Dans mon code, j'ai besoin d'un bouton pour tout sélectionner ou désélectionner (les lignes), d'un bouton pour sélectionner ou déselectionner les lignes de mon choix (je choisis la ligne par un double click), et d'un bouton pour vider mon stringgrid en ne gardant que les lignes désélectionner.

    Il faut aussi que les couleurs de mes lignes soit différente une fois sur 2 (chez moi jaunes et blanches) pour une meilleur lisibilité.

    Il faut vraiment que je revois ma copie car c'est plein de bug et compliqué pour l'utilisateur final.

    A plus tard.

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Re-bonjour,

    J'ai modifié mon code et cela marche à part les carractères de ma colonne 0 qui sont cachés lors de la sélection de façon complétement aléatoire.

    Par contre je n'arrive plus à avoir une ligne sur deux de couleurs différente par exemple les lignes paires en blanc et les lignes impaires en jaune. Ceci pour une meilleur lisibilité de mon stringgrid.

    Voici mon code actuel :


    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
     
     
    var
        Iselect:array of boolean;
     
    procedure TFlistfact.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
     
    begin
     
     
     
      With Sender As TStringGrid Do With Canvas Do
      Begin
     
       setlength(ISelect,stringgrid1.RowCount);
     
        { sélection de la couleur de fond }
        If (ACol > 0 ) Then
        If ISelect[ARow] Then
           Begin
              Brush.Color := clRED ;
              Font.Color := clwhite ;
           End
        Else
           Begin
              Brush.Color := clBtnFace ;
              Font.Color :=  ClBlack ;
           End ;
        { Dessin du fond }
        FillRect(Rect);
        { Dessin du texte }
        TextOut(Rect.Left,Rect.Top,Cells[ACol,ARow]);
      End;
     
    procedure TFlistfact.Button6Click(Sender: TObject);
    var
      i:integer;
     
    begin
     
       setlength(ISelect,stringgrid1.RowCount);
       if (button5.Caption='&Tout Sélectionner') then
          begin
     
     
             For i := 1 To StringGrid1.RowCount-1 Do
                ISelect[i] := True ;
             StringGrid1.Invalidate ;
             Button5.Caption:='&Tout Désélectionner';
           end
       else
          begin
             For i := 1 To StringGrid1.RowCount-1 Do
                ISelect[i] := False ;
             StringGrid1.Invalidate ;
             Button5.Caption:='&Tout Sélectionner';
          end;
     
    end;
    Merci d'avance pour votre aide.

  13. #13
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Bonjour,

    avec ceci tu récupères tes couleurs de lignes paires et impaires ;

    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
    { ================================================================ }
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    Begin
     
      With Sender As TStringGrid Do With Canvas Do
      Begin
     
        { sélection de la couleur de fond }
        If (ACol > 0 ) Then
           If ISelect[ARow] Then
              Begin
                 Brush.Color := clRED ;
                 Font.Color := clwhite ;
              End
           Else
              Begin
                 if not Odd(ARow) then
                    Canvas.Brush.Color:=clwindow
                 Else
                    Canvas.Brush.Color:=clyellow;
                 Font.Color :=  ClBlack ;
              End  ;
        { Dessin du fond }
        FillRect(Rect);
        { Dessin du texte }
        TextOut(Rect.Left,Rect.Top,Cells[ACol,ARow]);
       End ;
     
    End;
    { ================================================================ }
    A+

    Charly

  14. #14
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Bonjour Charly,

    Ton code marche très bien, merci beaucoup.

    Par contre je n'arrive plus à changer la couleur d'une cellule que je sélectionne et je n'arrive plus à sélectionner une ligne en double cliquant sur une cellule de la colonne 0.

    J'ai ajouté ce code dans OnDrawCell qui déjà me permet de me positionner sur la cellule de mon choix. Mais pour le reste....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
     
    if gdfocused in state then
     
        stringgrid1.Canvas.DrawFocusRect(rect);
    Merci d'avance.

  15. #15
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Bon alors, tu créés 2 variables Col1 et Row1 de type LongInt et tu complètes comme ceci (en affectant les fonctions StringGrid1DblClick et StringGrid1MouseDown à la grille)

    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
    { ================================================================ }
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    Begin
      With Sender As TStringGrid Do With Canvas Do
      Begin
    
        { sélection de la couleur de fond }
        If (ACol > 0 ) Then
           If ISelect[ARow] Then
              Begin
                 Brush.Color := clRED ;
                 Font.Color := clwhite ;
              End
           Else
              Begin
                 if not Odd(ARow) then
                    Canvas.Brush.Color:=clwindow
                 Else
                    Canvas.Brush.Color:=clyellow;
                 Font.Color :=  ClBlack ;
              End  ;
        { couleur de la cellule sélectionnée }
        If gdfocused in state then
           Begin
              Canvas.Brush.Color:=clblue;
              Font.Color :=  ClWhite ;
           End ;
        { Dessin du fond }
        FillRect(Rect);
        { Dessin du texte }
        TextOut(Rect.Left,Rect.Top,Cells[ACol,ARow]);
       End ;
    
    end;
    { ================================================================ }
    procedure TForm1.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
       Stringgrid1.MouseToCell(X, Y, Col1, Row1);
    end;
    { ================================================================ }
    procedure TForm1.StringGrid1DblClick(Sender: TObject);
    begin
       If (Row1 > 0) Then
       If (Col1 = 0) Then
          Begin
             ISelect[Row1] := Not ISelect[Row1] ;
             StringGrid1.Invalidate ;
          End ;
    end;
    { ================================================================ }

Discussions similaires

  1. [StringGrid] Sélection de toutes les lignes
    Par masterdash dans le forum Langage
    Réponses: 4
    Dernier message: 03/01/2013, 08h36
  2. [XL-2000] ListView Sélection/Déselection 1er ligne
    Par fidecourt dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 03/04/2009, 10h41
  3. Sélection de la Xième ligne à la Yième ligne
    Par Stéph59 dans le forum Langage SQL
    Réponses: 3
    Dernier message: 27/07/2004, 17h18
  4. Déplacer la sélection d'une ligne dans un stringgrid
    Par jer64 dans le forum Composants VCL
    Réponses: 5
    Dernier message: 14/03/2003, 00h57
  5. Multi lignes dans un StringGrids ?
    Par Xavier dans le forum C++Builder
    Réponses: 3
    Dernier message: 27/11/2002, 23h15

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