ryan_sabarre
asked on
delete a row in stringgrid
How can i delete a row in a stringgrid?
ASKER
and also how to sort an specified field in a stringgrid
TheStringGrid.Rows.Delete( row_number );
For sorting, unless someone gives you a better solution, you'll have to implement some sorting algorithm over TheStringGrid.Rows and use the Exchange method of TheStringGrid.Rows for exchanging items during sort.
HTH,
F.
For sorting, unless someone gives you a better solution, you'll have to implement some sorting algorithm over TheStringGrid.Rows and use the Exchange method of TheStringGrid.Rows for exchanging items during sort.
HTH,
F.
a sample for sorting, digged out
unit stringgridsort_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
CheckBox1: TCheckBox;
procedure StringGrid1MouseDown(Sende r: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender : TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
FSortedColIndex : Integer;
FSortOrder : Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Var AsIntegerFlag : Boolean;
Function SpecialSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := 0;
try
case AsIntegerFlag of
//AnsiSort
False : If List[Index1] < List[Index2] then Result := -1
else If List[Index1] > List[Index2] then Result := 1;
//IntegerSort
True : If StrToInt(List[Index1]) < StrToInt(List[Index2]) then Result := -1
else If StrToInt(List[Index1]) > StrToInt(List[Index2]) then Result := 1;
end;
except
Result := 0; //No Sort on Error;
end;
end;
Procedure SortGrid(AGrid : TStringGrid; ColIndex : Integer; SortOrder : Boolean;
//Optional Parameters
UseCustomSort : Boolean = False;
CustomSortProc : TStringListSortCompare = Nil);
//SortOrder=true->Ascendin g,SortOrde r=False->D escending
//first row is excluded from the sort
var
SL : TStringList;
ARow : TStringList;
I : Integer;
begin
if (assigned(AGrid)) and
(ColIndex > -1) and
(ColIndex < AGrid.ColCount) then
begin
SL := TStringList.Create;
try
For I := 1 to AGrid.RowCount - 1 do
begin
ARow := TStringList.Create;
ARow.Assign(AGrid.Rows[i]) ;
SL.AddObject(AGrid.Cells[C olIndex,I] ,ARow);
end;
if useCustomSort and assigned(CustomSortProc) then
try
SL.CustomSort(CustomSortPr oc); //Userdefined sort
except
raise;
end
else
SL.Sorted := true; //AnsiCompareString
Case SortOrder of
True : For I := 0 to SL.Count - 1 do
if Assigned(SL.Objects[I]) then
AGrid.Rows[I+1].Assign(TSt ringList(S L.Objects[ I]));
False : For I := SL.Count - 1 downto 0 do
if Assigned(SL.Objects[I]) then
AGrid.Rows[AGrid.RowCount - I - 1].Assign(TStringList(SL.O bjects[I]) );
end;
finally
for I := 0 to sl.count-1 do
if Assigned(SL.Objects[I]) then
begin
TStringList(SL.Objects[I]) .Free;
SL.Objects[I] := Nil;
end;
sl.free;
end;
end;
end;
Function RandomString(Lngth : Integer; CharSet : TSysCharset) : String;
var C : Char;
begin
Result := '';
if (lngth > 0) and not (CharSet = []) then
while length(Result) < lngth do
begin
Repeat
C := Chr(Random(255));
Until C in CharSet;
Result := Result + C;
end;
end;
procedure TForm1.StringGrid1MouseDow n(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
ACol,ARow : Integer;
ASortProc : TStringListSortCompare;
begin
ASortProc := SpecialSort;
StringGrid1.MouseToCell(x, y,ACol,ARo w);
AsIntegerFlag := ACol in [1,2];
if (ARow = 0) and
(ACol > 0) and
(Acol < StringGrid1.RowCount) then
if Button = mbleft then
begin
FSortedColIndex := ACol;
FSortOrder := True;
SortGrid(StringGrid1,ACol, True,Check box1.Check ed,ASortPr oc) //asc on columntitle-leftclick
end
else
begin
FSortedColIndex := ACol;
FSortOrder := False;
SortGrid(StringGrid1,ACol, False,Chec kbox1.Chec ked,ASortP roc);//Des c on columntitle-rightclick
end;
end;
//Create a unsorted content
procedure TForm1.Button1Click(Sender : TObject);
var I : Integer;
begin
StringGrid1.RowCount := 10001;
StringGrid1.ColCount := 7;
for i := 1 to 6 do
StringGrid1.Cells[I,0] := Randomstring(5,['0'..'9',' A'..'Z','a '..'z']);
for i := 1 to 10000 do //10000 Rows
begin
StringGrid1.Cells[1,I] := IntToStr(I);
StringGrid1.Cells[2,I] := IntToStr(10001-I);
StringGrid1.Cells[3,I] := Randomstring(10,['A'..'Z'] );
StringGrid1.Cells[4,I] := Randomstring(10,['0'..'9', 'A'..'Z',' a'..'z']);
StringGrid1.Cells[5,I] := Randomstring(10,[#0..#255] );
StringGrid1.Cells[6,I] := '1000';
end;
end;
//Draw a Triangle additional to the Caption
procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
const CMargin = 5;
begin
If (ARow = 0) and (ACol = FSortedColIndex) then //FSortedColIndex->the col, which is sorted
case FSortOrder of
true : begin //Ascending
StringGrid1.Canvas.Pen.Col or := clbtnShadow;
StringGrid1.Canvas.MoveTo( Rect.Right -CMargin,R ect.Top+CM argin);
StringGrid1.Canvas.LineTo( Rect.Right -7-CMargin ,Rect.Top+ CMargin);
StringGrid1.Canvas.LineTo( Rect.Right -4-CMargin ,Rect.Top+ 7+CMargin) ;
StringGrid1.Canvas.Pen.Col or := clBtnHighLight;
StringGrid1.Canvas.LineTo( Rect.Right -CMargin,R ect.Top+CM argin);
end;
false : begin //Descending
StringGrid1.Canvas.Pen.Col or := clBtnHighLight;
StringGrid1.Canvas.MoveTo( Rect.Right -4-CMargin ,Rect.Top+ CMargin);
StringGrid1.Canvas.LineTo( Rect.Right -CMargin,R ect.Top+7+ CMargin);
StringGrid1.Canvas.LineTo( Rect.Right -7-CMargin ,Rect.Top+ 7+CMargin) ;
StringGrid1.Canvas.Pen.Col or := clbtnShadow;
StringGrid1.Canvas.LineTo( Rect.Right -4-CMargin ,Rect.Top+ CMargin);
end;
end;
end;
initialization
Randomize;
end.
meikl ;-)
unit stringgridsort_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
CheckBox1: TCheckBox;
procedure StringGrid1MouseDown(Sende
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender
Rect: TRect; State: TGridDrawState);
private
FSortedColIndex : Integer;
FSortOrder : Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Var AsIntegerFlag : Boolean;
Function SpecialSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := 0;
try
case AsIntegerFlag of
//AnsiSort
False : If List[Index1] < List[Index2] then Result := -1
else If List[Index1] > List[Index2] then Result := 1;
//IntegerSort
True : If StrToInt(List[Index1]) < StrToInt(List[Index2]) then Result := -1
else If StrToInt(List[Index1]) > StrToInt(List[Index2]) then Result := 1;
end;
except
Result := 0; //No Sort on Error;
end;
end;
Procedure SortGrid(AGrid : TStringGrid; ColIndex : Integer; SortOrder : Boolean;
//Optional Parameters
UseCustomSort : Boolean = False;
CustomSortProc : TStringListSortCompare = Nil);
//SortOrder=true->Ascendin
//first row is excluded from the sort
var
SL : TStringList;
ARow : TStringList;
I : Integer;
begin
if (assigned(AGrid)) and
(ColIndex > -1) and
(ColIndex < AGrid.ColCount) then
begin
SL := TStringList.Create;
try
For I := 1 to AGrid.RowCount - 1 do
begin
ARow := TStringList.Create;
ARow.Assign(AGrid.Rows[i])
SL.AddObject(AGrid.Cells[C
end;
if useCustomSort and assigned(CustomSortProc) then
try
SL.CustomSort(CustomSortPr
except
raise;
end
else
SL.Sorted := true; //AnsiCompareString
Case SortOrder of
True : For I := 0 to SL.Count - 1 do
if Assigned(SL.Objects[I]) then
AGrid.Rows[I+1].Assign(TSt
False : For I := SL.Count - 1 downto 0 do
if Assigned(SL.Objects[I]) then
AGrid.Rows[AGrid.RowCount - I - 1].Assign(TStringList(SL.O
end;
finally
for I := 0 to sl.count-1 do
if Assigned(SL.Objects[I]) then
begin
TStringList(SL.Objects[I])
SL.Objects[I] := Nil;
end;
sl.free;
end;
end;
end;
Function RandomString(Lngth : Integer; CharSet : TSysCharset) : String;
var C : Char;
begin
Result := '';
if (lngth > 0) and not (CharSet = []) then
while length(Result) < lngth do
begin
Repeat
C := Chr(Random(255));
Until C in CharSet;
Result := Result + C;
end;
end;
procedure TForm1.StringGrid1MouseDow
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
ACol,ARow : Integer;
ASortProc : TStringListSortCompare;
begin
ASortProc := SpecialSort;
StringGrid1.MouseToCell(x,
AsIntegerFlag := ACol in [1,2];
if (ARow = 0) and
(ACol > 0) and
(Acol < StringGrid1.RowCount) then
if Button = mbleft then
begin
FSortedColIndex := ACol;
FSortOrder := True;
SortGrid(StringGrid1,ACol,
end
else
begin
FSortedColIndex := ACol;
FSortOrder := False;
SortGrid(StringGrid1,ACol,
end;
end;
//Create a unsorted content
procedure TForm1.Button1Click(Sender
var I : Integer;
begin
StringGrid1.RowCount := 10001;
StringGrid1.ColCount := 7;
for i := 1 to 6 do
StringGrid1.Cells[I,0] := Randomstring(5,['0'..'9','
for i := 1 to 10000 do //10000 Rows
begin
StringGrid1.Cells[1,I] := IntToStr(I);
StringGrid1.Cells[2,I] := IntToStr(10001-I);
StringGrid1.Cells[3,I] := Randomstring(10,['A'..'Z']
StringGrid1.Cells[4,I] := Randomstring(10,['0'..'9',
StringGrid1.Cells[5,I] := Randomstring(10,[#0..#255]
StringGrid1.Cells[6,I] := '1000';
end;
end;
//Draw a Triangle additional to the Caption
procedure TForm1.StringGrid1DrawCell
Rect: TRect; State: TGridDrawState);
const CMargin = 5;
begin
If (ARow = 0) and (ACol = FSortedColIndex) then //FSortedColIndex->the col, which is sorted
case FSortOrder of
true : begin //Ascending
StringGrid1.Canvas.Pen.Col
StringGrid1.Canvas.MoveTo(
StringGrid1.Canvas.LineTo(
StringGrid1.Canvas.LineTo(
StringGrid1.Canvas.Pen.Col
StringGrid1.Canvas.LineTo(
end;
false : begin //Descending
StringGrid1.Canvas.Pen.Col
StringGrid1.Canvas.MoveTo(
StringGrid1.Canvas.LineTo(
StringGrid1.Canvas.LineTo(
StringGrid1.Canvas.Pen.Col
StringGrid1.Canvas.LineTo(
end;
end;
end;
initialization
Randomize;
end.
meikl ;-)
ASKER
HELP ME
ASKER
please how to DELETE a row in a stringgrid
=fva= you gave me a wrong answer
your code was not recognized
delphi ask for '[' in Rows
and you given me this one.
TheStringGrid.Rows.Delete( row_number );
=fva= you gave me a wrong answer
your code was not recognized
delphi ask for '[' in Rows
and you given me this one.
TheStringGrid.Rows.Delete(
? maybe you should explain more
Deeply sorry. My fault. I didn't test the code, just wrote it directly. I'll try to find something better.
F.
F.
ASKER
its ok
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you so so much fva
Don't mention it. Delete the other question with the same title and having 50 pts hanging. You'll get back the points and probably fast since no one commented it yet. Thank you for accepting my comments.
BTW, you should have requested in Community Support for a point split on this one instead of giving me 100 and posting another 50 for Meikl.
Yours,
F.
BTW, you should have requested in Community Support for a point split on this one instead of giving me 100 and posting another 50 for Meikl.
Yours,
F.
an old trick you can use is the protected DeleteRow method
i know this is already answered but i had this on the clipboard so ... ;-)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TDelGrid = Class( TStringGrid);
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender : TObject);
begin
with TDelGrid(StringGrid1) do
DeleteRow( Stringgrid1.Row );
end;
end.
i know this is already answered but i had this on the clipboard so ... ;-)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TDelGrid = Class( TStringGrid);
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender
begin
with TDelGrid(StringGrid1) do
DeleteRow( Stringgrid1.Row );
end;
end.
Hi inthe,
Just curious (2 issues):
1. Am I missing something here? (I'm in a D3 shop)
Looks like you didn't expose DeleteRow in TDelGrid definition.
and
2. How did you get to comment a PAQ (haven't seen you on the comment list)? I guess you didn't buy it just for that.
Cheers,
F.
Just curious (2 issues):
1. Am I missing something here? (I'm in a D3 shop)
Looks like you didn't expose DeleteRow in TDelGrid definition.
and
2. How did you get to comment a PAQ (haven't seen you on the comment list)? I guess you didn't buy it just for that.
Cheers,
F.
ASKER