Comment déplacer (vers le haut et vers le bas) une ligne dans un stringGrid?

Résolu
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007 - 5 août 2005 à 13:46
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007 - 6 août 2005 à 10:25
salut a tous je cherche comment déplacer (vers le haut et vers le bas) une ligne dans un stringgrid en cliquant tout simplement sur un bouton (toutes les lignes du stringgrid doivent être déplaçables), je ne veux pas passer par les navigateur de base de données, merci

++

7 réponses

Gildeplomb Messages postés 101 Date d'inscription samedi 29 décembre 2012 Statut Membre Dernière intervention 1 novembre 2023
6 août 2005 à 00:39
Exemple de code : ( faut ajouter uine verification des limites StringGrid )

procedure TForm1.btMonterLigneClick(Sender: TObject);
Var temp: String;
begin
Temp:= StringGrid1.Rows[StringGrid1.Row-1].Text;
StringGrid1.Rows[StringGrid1.Row-1].Text:=StringGrid1.Rows[StringGrid1.Row].text;
StringGrid1.Rows[StringGrid1.Row].text:= temp;
StringGrid1.Row:= StringGrid1.Row-1;
end;



procedure TForm1.btDescendClick(Sender: TObject);
var temp : string;
begin
Temp:= StringGrid1.Rows[StringGrid1.Row+1].Text;
StringGrid1.Rows[StringGrid1.Row+1].Text:=StringGrid1.Rows[StringGrid1.Row].text;
StringGrid1.Rows[StringGrid1.Row].text:= temp;
StringGrid1.Row:= StringGrid1.Row+1 ;
end;

Gil
3
cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 123
5 août 2005 à 16:20
Alors la tu dois tout simplement le faire manuellement:

Pose une StringGrid et 2 boutons et met ensuite ceci:



var

Form1: TForm1;

Selection:integer;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

var

Tmp:string;

begin

If Selection < StringGrid1.RowCount-1 then

begin

Tmp:= StringGrid1.Cells[1,Selection+1];

StringGrid1.Cells[1,Selection+1]:=StringGrid1.Cells[1,Selection];

StringGrid1.Cells[1,Selection]:=Tmp;

end ;

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

StringGrid1.Cells[1,1]: ='Test1';

StringGrid1.Cells[1,2]:= 'Test2';

StringGrid1.Cells[1,3]:='Test3';

StringGrid1.Cells[1,4]:='Test4';

end ;



procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,

ARow: Integer; var CanSelect: Boolean);

begin

Selection: =ARow;

end;



procedure TForm1.Bouton2Click(Sender: TObject);

var

Tmp:string;

begin

If Selection > 1 then

begin

Tmp:=StringGrid1.Cells[1,Selection-1];

StringGrid1.Cells[1,Selection-1]:=StringGrid1.Cells[1,Selection];

StringGrid1.Cells[1,Selection]:=Tmp;

end;

end;
0
Gildeplomb Messages postés 101 Date d'inscription samedi 29 décembre 2012 Statut Membre Dernière intervention 1 novembre 2023
5 août 2005 à 17:13
Bonjour

Pour inverser 2 rangées d'un StringGrig :

Procedure Tform1.inverse( r1, r2: Integer);
Var temp: String;
Begin
Temp:=StringGrid1.Rows[r1].Text;
StringGrid1.Rows[r1]:= StringGrid1.Rows[r2];
StringGrid1.Rows[r2].Text:=Temp;
End;

Procedure TForm1.Button1Click(Sender: TObject);
Var
temp : String;
r1,r2 : integer;
Begin
r1:=1; // Par exemple inverse ragée 1 et 2
r2:=2;
Inverse(r1, r2)
End;
0
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
5 août 2005 à 18:46
salut
voici mon code, peut tu me dire ou exactement placer ton code(il faut que ToolButton12 descende la ligne séléctionnée et que ToolButton13 remonte la ligne selectionnée) en essayant le code de Cpt Pingu il descend juste la cellule et en plus on ne peut pas répéter cette procedure sur la meme ligne:

unit Unit1;


interface


uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Grids, StdActns, ActnList, Menus,
ImgList, ToolWin,Printers, BaseC,IniFiles;


type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Edit4: TEdit;
Button1: TButton;
Edit3: TEdit;
StatusBar1: TStatusBar;
GroupBox1: TGroupBox;
StringGrid1: TStringGrid;
ActionList1: TActionList;
EditCut1: TEditCut;
EditCopy1: TEditCopy;
EditPaste1: TEditPaste;
EditSelectAll1: TEditSelectAll;
EditUndo1: TEditUndo;
EditDelete1: TEditDelete;
FileOpen1: TFileOpen;
FileSaveAs1: TFileSaveAs;
FilePrintSetup1: TFilePrintSetup;
FileExit1: TFileExit;
Enregistrer: TAction;
MainMenu1: TMainMenu;
Fichier1: TMenuItem;
Enregistrersous1: TMenuItem;
Ouvrir1: TMenuItem;
N1: TMenuItem;
Configurerlimpression1: TMenuItem;
N2: TMenuItem;
Quitter1: TMenuItem;
Edition1: TMenuItem;
Couper1: TMenuItem;
Copier1: TMenuItem;
Coller1: TMenuItem;
N3: TMenuItem;
Supprimer1: TMenuItem;
outslectionner1: TMenuItem;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
StaticText4: TStaticText;
PopupMenu1: TPopupMenu;
Couper2: TMenuItem;
Copier2: TMenuItem;
Coller2: TMenuItem;
N4: TMenuItem;
Supprimer2: TMenuItem;
outslectionner2: TMenuItem;
Dfaire1: TMenuItem;
N5: TMenuItem;
Dfaire2: TMenuItem;
N6: TMenuItem;
ImageList1: TImageList;
Label5: TLabel;
ComboBox1: TComboBox;
Label6: TLabel;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
ToolButton10: TToolButton;
ToolButton11: TToolButton;
StaticText5: TStaticText;
Imprimer: TMenuItem;
Nouveau: TAction;
ToolBar2: TToolBar;
ToolButton14: TToolButton;
ToolButton15: TToolButton;
ImageList2: TImageList;
Lecture: TOpenDialog;
Ecriture: TSaveDialog;
ToolButton18: TToolButton;
ToolButton19: TToolButton;
Aide1: TMenuItem;
Aide2: TMenuItem;
Apropos1: TMenuItem;
ToolButton12: TToolButton;
ToolButton13: TToolButton;
procedure Button1Click(Sender: TObject);
procedure ToolButton3Click(Sender: TObject);
procedure ImprimerClick(Sender: TObject);
procedure ToolButton14Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure ToolButton15Click(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton19Click(Sender: TObject);
procedure Apropos1Click(Sender: TObject);


private
{ Déclarations privées }
public
{ Déclarations publiques }


end;


var
Form1: TForm1;


implementation
uses Unit2;
{$R *.dfm}



Procedure EcritureGrille(Grille:TStringGrid;NomFichier:String;CasesVides:Boolean=True);
Var Ini:TIniFile;
i,j:Integer;
begin
Ini:=TIniFile.Create(NomFichier);
With Grille Do Try
Ini.WriteInteger('GENERAL','RowCount',RowCount);
Ini.WriteInteger('GENERAL','ColCount',ColCount);
For i:=0 To ColCount-1 Do For j:=0 To RowCount-1 Do
Ini.WriteString(IntToStr(i),IntToStr(j),Cells[i,j]);
Finally
Ini.Free;
End;
End;



Procedure LectureGrille(Grille:TStringGrid;NomFichier:String);
Var Ini:TIniFile;
i,j:Integer;
begin
Ini:=TIniFile.Create(NomFichier);
With Grille Do Try
If INI.ValueExists('GENERAL','RowCount')And
INI.ValueExists('GENERAL','ColCount')
Then Begin
RowCount:=Ini.ReadInteger('GENERAL','RowCount',RowCount);
ColCount:=Ini.ReadInteger('GENERAL','ColCount',ColCount);
For i:=0 To ColCount-1 Do For j:=0 To RowCount-1 Do
Cells[i,j]:=Ini.ReadString(IntToStr(i),IntToStr(j),'');
End;
Finally
Ini.Free;
End;
End;



procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
var
X1, X2: Integer;
Y1, Y2: Integer;
TmpI: Integer;
F: Integer;
TR: TRect;
begin
Printer.Title := sTitle;
Printer.BeginDoc;
Printer.Canvas.Pen.Color := 0;
Printer.Canvas.Font.Name := 'Times New Roman';
Printer.Canvas.Font.Size := 10;
Printer.Canvas.Font.Style := [fsBold, fsUnderline];
Printer.Canvas.TextOut(0, 100, Printer.Title);
for F := 1 to sGrid.ColCount - 1 do
begin
X1 := 0;
for TmpI := 1 to (F - 1) do
X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
Y1 := 300;
X2 := 0;
for TmpI := 1 to F do
X2 := X2 + 5 * (sGrid.ColWidths[TmpI]);
Y2 := 450;
TR := Rect(X1, Y1, X2 - 30, Y2);
Printer.Canvas.Font.Style := [fsBold];
Printer.Canvas.Font.Size := 7;
Printer.Canvas.TextRect(TR, X1 + 50, 350, sGrid.Cells[F, 0]);
Printer.Canvas.Font.Style := [];
for TmpI := 1 to sGrid.RowCount - 1 do
begin
Y1 := 150 * TmpI + 300;
Y2 := 150 * (TmpI + 1) + 300;
TR := Rect(X1, Y1, X2 - 30, Y2);
Printer.Canvas.TextRect(TR, X1 + 50, Y1 + 50, sGrid.Cells[F, TmpI]);
end;
end;
Printer.EndDoc;
end;



procedure TForm1.Button1Click(Sender: TObject);
Var Rangee : integer;
begin
Rangee:= StringGrid1.RowCount-1; // derniere rangée
Form1.StringGrid1.Cells[0,Rangee]:=Edit1.Text;
Form1.StringGrid1.Cells[1,Rangee]:=Edit2.Text;
Form1.StringGrid1.Cells[2,Rangee]:=Edit3.Text;
Form1.StringGrid1.Cells[3,Rangee]:=Edit4.Text;
Form1.StringGrid1.Cells[4,Rangee]:=Combobox1.Text;
StringGrid1.RowCount:= StringGrid1.RowCount+1;
Form1.Edit1.Clear;
Form1.Edit2.Clear;
Form1.Edit3.Clear;
Form1.Edit4.Clear;
Form1.ComboBox1.ClearSelection;
end;


procedure TForm1.ToolButton3Click(Sender: TObject);
begin
PrintGrid(StringGrid1, 'Print Stringgrid');
end;
procedure TForm1.ImprimerClick(Sender: TObject);
begin
PrintGrid(StringGrid1, 'Print Stringgrid');
end;



procedure TForm1.ToolButton14Click(Sender: TObject);
var i:integer;
begin


for i := StringGrid1.row to StringGrid1.RowCount - 2 do
StringGrid1.Rows[i] := StringGrid1.Rows[i + 1];
StringGrid1.RowCount := StringGrid1.RowCount - 1;
end;


procedure TForm1.ToolButton2Click(Sender: TObject);
begin
If Not Ecriture.Execute Then Exit;
EcritureGrille(StringGrid1,Ecriture.FileName,
MessageDlg('Voulez vous enregistrer les lignes vides ?',
mtConfirmation,[mbYes,MbNo],0)=mrYes);
end;


procedure TForm1.ToolButton15Click(Sender: TObject);
begin
If Not Ecriture.Execute Then Exit;
EcritureGrille(StringGrid1,Ecriture.FileName,
MessageDlg('Voulez vous enregistrer les lignes vides ?',
mtConfirmation,[mbYes,MbNo],0)=mrYes);
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
If Not Lecture.Execute Then Exit;
LectureGrille(StringGrid1,Lecture.FileName);
end;


procedure TForm1.ToolButton19Click(Sender: TObject);
var
i:integer;
begin


for i := 0 to stringgrid1.rowcount-1 do


Stringgrid1.rows[i].clear;


Stringgrid1.rowcount:=0;


end;



procedure TForm1.Apropos1Click(Sender: TObject);
begin
AboutBox.ShowModal;
end;



end.


merci, bye
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Gildeplomb Messages postés 101 Date d'inscription samedi 29 décembre 2012 Statut Membre Dernière intervention 1 novembre 2023
5 août 2005 à 21:02
Faudrait etre un peu plus clair

Descendre la ligne selectionnée c'est quoi ?
remplacer la ligne qui suit par celle selectionnée ? c'est a dire effacer la suivante ?
ajouter une ligne ?
remonter c'est quoi ? supprimer la ligne precedente ?

Essaye de bien expliquer ce que tu veux ( et quelques commentaires dans ton code seraient les bienvenus

Gil
0
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
5 août 2005 à 21:39
je vais etre plus précis:
j'ai besoin d'un code qui en appuyant sur un bouton descend la ligne( pour choisir l'ordre )qui est séléctionnée dans le stringgrid (comme dans les bases de données) et un autre bouton qui fait linverse,laisse tomber le code que j'ai posté ca creer des confusions;jattens juste la commande qui permet de metre lordre comme précédemment indiqué. merci
++
0
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
6 août 2005 à 10:25
merci beaucoup gildeplomb ca a marché
0
Rejoignez-nous