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:StringGrid1->RowCount-- ;
mais cela me supprime la dernière ligne.
Pouvez-vous m'aider? Merci.
Version imprimable
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:StringGrid1->RowCount-- ;
mais cela me supprime la dernière ligne.
Pouvez-vous m'aider? Merci.
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.
Jen ne veux pas supprimer la dernière ligne mais la ligne sélectionnée
Salut !
Ce qui revient à faire, comme l'indique bakaneko :
Si (n) est la ligne à supprimer :
A plus !Code:
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--;
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
StrGrid.hCode:
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); } //---------------------------------------------------------------------------
Pour l'utilisation, un ptit exemple:Code:
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
En espérant que ça aide plusieurs personnesCode:
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 } //---------------------------------------------------------------------------
Voili voilou
A+
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'
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.
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
Unit2.cppCode:
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); } //------------------------------------------------------------------#
Code:
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:
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 #
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:
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--;
Bonsoir,
J'en rajoute une couche avec une autre proposition...
Dans l'évènement OnDrawCell du TStringGrid:
Dans le .h de la form:Code:
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]);
Puis pour faire varier la valeur de 'Effacer', par exemple (uniquement), j'utilise un petit TCheckBox avec dans l'évènement OnClick :Code:
1
2
3 private: // Déclarations de l'utilisateur bool Effacer;
Ca marche même si l'on laisse la propriété du TStringGrid DefaultDraw a true.Code:
1
2
3
4
5 void __fastcall TForm1::CheckBox1Click(TObject *Sender) { Effacer = CheckBox1->Checked; }
@ + ;)