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

C++Builder Discussion :

supprimer une ligne dans un tableau [Non suivi]


Sujet :

C++Builder

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    Points : 63
    Points
    63
    Par défaut supprimer une ligne dans un tableau
    Bonjour, j'aimerai supprimer une ligne se trouvant dans un milieu d'un tableau mais je n'y arrive pas. En effet, j'ultilise

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     StringGrid1->RowCount-- ;

    mais cela me supprime la dernière ligne.

    Pouvez-vous m'aider? Merci.

  2. #2
    Membre expérimenté
    Avatar de bakaneko
    Profil pro
    Inscrit en
    Février 2004
    Messages
    1 268
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 1 268
    Points : 1 427
    Points
    1 427
    Par défaut
    Tu peux parcourir ton tableau et décaler chaque ligne à partir de ton indice et supprimer la dernière ligne.

    Donc la ligne à la position indice+1 devient la ligne à la position indice, la ligne se trouvant à la position indice+2 devient la ligne à la position indice+1, etc.
    Ensuite, tu supprimes la dernière ligne.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    Points : 63
    Points
    63
    Par défaut re
    Jen ne veux pas supprimer la dernière ligne mais la ligne sélectionnée

  4. #4
    Membre chevronné

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    1 390
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 390
    Points : 1 777
    Points
    1 777
    Par défaut
    Salut !

    Ce qui revient à faire, comme l'indique bakaneko :

    Si (n) est la ligne à supprimer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    int max = StringGrid1->RowCount-1;
    for(int row = n; row < max; row++)
        {
        StringGrid1->Rows[row] = StringGrid1->Rows[row+1];
        }
    StringGrid1->RowCount--;
    A plus !

  5. #5
    Membre du Club

    Inscrit en
    Août 2002
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 24
    Points : 52
    Points
    52
    Par défaut
    Salut,

    J'ai une autre méthode pour toi. J'ai trouvé ca sur le net.

    On va se créer une classe qui va hériter de certaines fonctions du TCustomGrid qui ne sont pas accessibles à partir d'un TStringGrid.

    Il faut se créer 2 fichiers.

    StrGrid.cpp
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #pragma package(smart_init)
     
    #include "StrGrid.h"
     
    //---------------------------------------------------------------------------
    __fastcall TStrGrid::TStrGrid(TComponent* Owner)
                                :TStringGrid(Owner)
    {
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TStrGrid::DeleteRow(int Row)
    {
        TCustomGrid::DeleteRow(Row);
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TStrGrid::DeleteCol(int Col)
    {
        TCustomGrid::DeleteColumn(Col);
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TStrGrid::MoveRow(int FromIndex, int ToIndex)
    {
        TCustomGrid::MoveRow(FromIndex, ToIndex);
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TStrGrid::MoveColumn(int FromIndex, int ToIndex)
    {
        TCustomGrid::MoveColumn(FromIndex, ToIndex);
    }
    //---------------------------------------------------------------------------
    StrGrid.h
    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
     
    //-----------------------------------------------------------------------
    #ifndef StrGridH
    #define StrGridH
    #include <grids.hpp>
     
    class TStrGrid: public TStringGrid
    {
    public:
        __fastcall TStrGrid(TComponent* Owner);
        void __fastcall DeleteRow(int Row);
        void __fastcall DeleteCol(int Col);
        void __fastcall MoveRow(int FromIndex, int ToIndex);
        void __fastcall MoveColumn(int FromIndex, int ToIndex);
    };
     
    //----------------------------------------------------------------------
    #endif
    Pour l'utilisation, un ptit exemple:
    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
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    #include "StrGrid.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        TStrGrid * MyStrGrid = (TStrGrid *)StringGrid1;
    
        MyStrGrid->DeleteRow(5);        // Supprime la ligne 6
        MyStrGrid->DeleteCol(7);        // Supprime la colonne 8
        MyStrGrid->MoveRow(2, 10);      // Déplace la ligne 3 à la ligne 11
        MyStrGrid->MoveColumn(8, 1);    // Déplace la colonne 9 à la colonne 2
    }
    //---------------------------------------------------------------------------
    En espérant que ça aide plusieurs personnes

    Voili voilou
    A+

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    Points : 63
    Points
    63
    Par défaut re
    J'ai fai ce la deuxième méthode mais j'ai une erreur


    [C++ Erreur] StrGrid.cpp(22): E2316 '_fastcall TStrGrid::TStrGrid(TComponent *)' n'est pas un membre de 'TStrGrid'

  7. #7
    Membre du Club

    Inscrit en
    Août 2002
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 24
    Points : 52
    Points
    52
    Par défaut
    Si t'as ajouté StrGrid.cpp à ton projet, tu ne devrais pas avoir de problème de compilation. Je viens de faire le teste ça marche nikel.

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    Points : 63
    Points
    63
    Par défaut Re
    J'ai fai un copier coller de ce qui est écrit et moi cela ne marche pas. J'ai toujours la même erreur.

    StrGrid.cpp
    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
     #//---------------------------------------------------------------------------
    /*
    #pragma hdrstop
    #include "StrGrid.h"
     */
    //---------------------------------------------------------------------------
     
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #pragma package(smart_init)
    #include "StrGrid.h"
    //---------------------------------------------------------------------------
    __fastcall TStrGrid::TStrGrid(TComponent* Owner)
                                :TStringGrid(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TStrGrid::DeleteRow(int Row)
    {
        TCustomGrid::DeleteRow(Row);
    }
    //---------------------------------------------------------------------------
    void __fastcall TStrGrid::DeleteCol(int Col)
    {
        TCustomGrid::DeleteColumn(Col);
    }
    //---------------------------------------------------------------------------
    void __fastcall TStrGrid::MoveRow(int FromIndex, int ToIndex)
    {
        TCustomGrid::MoveRow(FromIndex, ToIndex);
    }
    //---------------------------------------------------------------------------
    void __fastcall TStrGrid::MoveColumn(int FromIndex, int ToIndex)
    {
        TCustomGrid::MoveColumn(FromIndex, ToIndex);
    }
    //------------------------------------------------------------------#
    Unit2.cpp

    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
     #//---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit2.h"
    #include "Unit3.h"
    #include "Unit4.h"
    #include "Unit5.h"
    #include "StrGrid.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm2 *Form2;
    //---------------------------------------------------------------------------
    __fastcall TForm2::TForm2(TComponent* Owner)
      : TForm(Owner)
    {   i=2;
        StringGrid1->Cells[0][0] = "N° VM";          //sert à afficher dans la case [0][0] de l'entête
        StringGrid1->Cells[1][0] = "Nom du projet";  //sert à afficher dans la case [1][0] de l'entête
        StringGrid1->Cells[2][0] = "Adresse IP";
        StringGrid1->Cells[3][0] = "Port";
        StringGrid1->FixedCols = 1 ;                //met en grisé la 1ere colonne
        StringGrid1->ColCount=4;                    //Ici, notre tableau sera composé de 4 colones
        StringGrid1->RowCount=2;                    //Ici, notre tableau sera composé de 2 lignes
        StringGrid1->Height=55;                     //permet de redéfinir note interface pour notre tableau pour la hauteur
        StringGrid1->Width=408;                     //permet de redéfinir note interface pour notre tableau pour la largeur
        StringGrid1->Cells[0][1] = "VM1";           //permet d'écrire "Vm1" dans la case[0][1]
     
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::DBGrid1KeyPress(TObject *Sender, char &Key)
    {
     
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm2::LabeledEdit1Change(TObject *Sender)
    {
     
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::AjouterClick(TObject *Sender)   //permet d'ajouter une ligne si l'utilisateur appuie sur le bouton '+'
    {                                                       //s'il veut rajouter une VM
        StringGrid1->Cells[0][i] ="VM"+String(i);          //permet d'afficher les VM i à chaque fois qu'on appuie sur '+'
       i++;
       StringGrid1->RowCount++;                           //rajoute une ligne
       StringGrid1->Height=(StringGrid1->Height+25);      //permet d'afficher les bonnes dimensions de l'interface
       StringGrid1->Width;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::RetirerClick(TObject *Sender)
    {
     
     try
    {
      //...
       if(StringGrid1->RowCount==2)                         // si il ne reste que 2 lignes on affiche un message d'erreur
        {
            //ShowMessage(AnsiString("Attention, vous ne pouvez pas supprimer cette ligne"));
            MessageBox(0,"Attention, vous ne pouvez pas supprimer cette ligne.\n", "Attention", MB_OK);
        }
      else
      {
        StringGrid1->RowCount--;                           //sinon on efface les lignes
        StringGrid1->Height=(StringGrid1->Height-25);
      }
    }
    catch(Exception &e)
    {
          ShowMessage("Erreur");
    }
     
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm2::StringGrid1SetEditText(TObject *Sender, int ACol,
          int ARow, const AnsiString Value)
    {
        StringGrid1->OnDblClick;
        Edit1->Text = Value;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::StringGrid1DblClick(TObject *Sender)   //permet d'écrire dans le tableau après avoir double cliquer dans une case
    {
         if(StringGrid1->Col==1)         //si on double-clique dans la 1ère colonne
         {  Form3->Show();               //On ouvre le formulaire correspondant
            goEditing==false;            //on interdit d'écrire dans le tableau
         }
         else if(StringGrid1->Col==2)    //si on double-clique dans la 2ème colonne
         {  Form4->Show();               //On ouvre le formulaire correspondant
            goEditing==false;            //on interdit d'écrire dans le tableau
         }
         else
         {                               //sinon si on double-clique dans la dernière colonne
           Form5->Show();                //On ouvre le formulaire correspondant
           goEditing==false;             //on interdit d'écrire dans le tableau
         }
     
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm2::EffacerClick(TObject *Sender)
    {
         TStrGrid * MyStrGrid = (TStrGrid *)StringGrid1;
     
        MyStrGrid->DeleteRow(5);        // Supprime la ligne 6
      //  StringGrid1->DeleteCol(7);        // Supprime la colonne 8
       // StringGrid1->MoveRow(2, 10);      // Déplace la ligne 3 à la ligne 11
       // StringGrid1->MoveColumn(8, 1);    // Déplace la colonne 9 à la colonne
     
    }
    //---------------------------------------------------------------------------
    #

    StrGrid.h

    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
     #//---------------------------------------------------------------------------
    #ifndef StrGridH
    #define StrGridH
    #include <grids.hpp>
    class TStrGrid: public TStringGrid
    {
    public:
        __fastcall TStrGrid1(TComponent* Owner);
        void __fastcall DeleteRow(int Row);
        void __fastcall DeleteCol(int Col);
        void __fastcall MoveRow(int FromIndex, int ToIndex);
        void __fastcall MoveColumn(int FromIndex, int ToIndex);
    };
    //----------------------------------------------------------------------
     
    //---------------------------------------------------------------------------
    #endif
    #

  9. #9
    Membre du Club

    Inscrit en
    Août 2002
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 24
    Points : 52
    Points
    52
    Par défaut
    Question con : t'as bien fais "Project -> Ajouter au projet..." ?

    Sinon il faudra que tu retournes à la première proposition, celle de bakaneko.
    Tu te crées une fonction qui à partir de la ligne qui sera supprimé tu recopies toutes les lignes suivantes d'un crant (donc, si ligne 2 est supprimée, la ligne 3 devient ligne 2, ligne 4 en ligne 3, etc)
    Pas très compliqué. C'est juste de la recopie de cellule. Ex :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    for(int i = index_de_la_ligne_a_del; i < StringGrid1->RowCount; i++)
    {
        StringGrid1->Cells[1][i] = StringGrid1->Cells[1][i+1];
    }
    StringGrid1->RowCount--;

  10. #10
    Membre expert
    Avatar de Sunchaser
    Homme Profil pro
    OPNI (Objet Programmant Non Identifié)
    Inscrit en
    Décembre 2004
    Messages
    2 059
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : OPNI (Objet Programmant Non Identifié)
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 059
    Points : 3 204
    Points
    3 204
    Par défaut
    Bonsoir,

    J'en rajoute une couche avec une autre proposition...

    Dans l'évènement OnDrawCell du TStringGrid:
    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
     
      StringGrid1->Canvas->FillRect(Rect);
      if (Effacer == true)
      {
      if (State.Contains(gdSelected))
      {
            for (int row = ARow; row < StringGrid1->RowCount-1; row++)
            {
            StringGrid1->Rows[row] = StringGrid1->Rows[row+1];
            }
            StringGrid1->RowCount--;
            Effacer = false; CheckBox1->Checked = false;
      }
      }
      StringGrid1->Canvas->TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1->Cells[ACol][ARow]);
    Dans le .h de la form:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    private:    // Déclarations de l'utilisateur
            bool Effacer;
    Puis pour faire varier la valeur de 'Effacer', par exemple (uniquement), j'utilise un petit TCheckBox avec dans l'évènement OnClick :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    void __fastcall TForm1::CheckBox1Click(TObject *Sender)
    {
    Effacer = CheckBox1->Checked;
    }
    Ca marche même si l'on laisse la propriété du TStringGrid DefaultDraw a true.

    @ +

Discussions similaires

  1. Supprimer une ligne dans un tableau
    Par mathurin2010 dans le forum AWT/Swing
    Réponses: 0
    Dernier message: 12/09/2014, 01h55
  2. Réponses: 1
    Dernier message: 28/03/2008, 21h37
  3. Supprimer une ligne dans un tableau
    Par Asdorve dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 28/06/2007, 15h14
  4. Réponses: 1
    Dernier message: 24/04/2007, 17h20

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