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 �clair�
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    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 Expert
    Avatar de bakaneko
    Profil pro
    Inscrit en
    F�vrier 2004
    Messages
    1 268
    D�tails du profil
    Informations personnelles :
    �ge : 45
    Localisation : France

    Informations forums :
    Inscription : F�vrier 2004
    Messages : 1 268
    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 �clair�
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    D�tails du profil
    Informations personnelles :
    Localisation : France

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

  4. #4
    Membre Expert

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

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 413
    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 averti

    Inscrit en
    Ao�t 2002
    Messages
    24
    D�tails du profil
    Informations forums :
    Inscription : Ao�t 2002
    Messages : 24
    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 �clair�
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    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 averti

    Inscrit en
    Ao�t 2002
    Messages
    24
    D�tails du profil
    Informations forums :
    Inscription : Ao�t 2002
    Messages : 24
    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 �clair�
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    342
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 342
    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 averti

    Inscrit en
    Ao�t 2002
    Messages
    24
    D�tails du profil
    Informations forums :
    Inscription : Ao�t 2002
    Messages : 24
    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 �prouv�
    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 : 54
    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
    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