How can I delete the row in TStringGrid component?
2009-05-07 15:31
351 查看
From: http://www.delphi3000.com/articles/article_819.asp?SK=
If you worked with TStringGrid component, then you saw that in this component the Borland developers not provided the method for row deleting.
In this tip I describe the few ways for it:
{ 1. navigate by rows and copy the row contains to the prev row: }
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i, j: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
for j := 0 to ColCount-1 do
Cells[j, i] := Cells[j, i+1];
RowCount := RowCount - 1
end;
end;
{ 2. the modificated #1: }
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
Rows[i].Assign(Rows[i+1]);
RowCount := RowCount - 1
end;
end;
{ 3. the "hacked" way. The TCustomGrid type (the TStringGrid is TCustomGrid's successor) have the DeleteRow method. But this method allocated not in public section but in protected section. So the all successors can "see" this DeleteRow method. }
type
THackStringGrid = class(TStringGrid);
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
begin
with THackStringGrid(yourStringGrid) do
DeleteRow(ARow);
end;
Personally I use the third method but the first and second are more visual.
If you worked with TStringGrid component, then you saw that in this component the Borland developers not provided the method for row deleting.
In this tip I describe the few ways for it:
{ 1. navigate by rows and copy the row contains to the prev row: }
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i, j: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
for j := 0 to ColCount-1 do
Cells[j, i] := Cells[j, i+1];
RowCount := RowCount - 1
end;
end;
{ 2. the modificated #1: }
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
Rows[i].Assign(Rows[i+1]);
RowCount := RowCount - 1
end;
end;
{ 3. the "hacked" way. The TCustomGrid type (the TStringGrid is TCustomGrid's successor) have the DeleteRow method. But this method allocated not in public section but in protected section. So the all successors can "see" this DeleteRow method. }
type
THackStringGrid = class(TStringGrid);
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
begin
with THackStringGrid(yourStringGrid) do
DeleteRow(ARow);
end;
Personally I use the third method but the first and second are more visual.
相关文章推荐
- 解决Error (1133): Can’t find any matching row in the user table
- How to troubleshoot the “Red Arrow” issue in Component Services (II)
- How You Can Cash In On The Green Gold Rush of 2014
- 《How hard can it be? Estimating the difficulty of visual search in an image》和 PPT
- How Can I Get the Unique ID for the Last Inserted Row
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- How to delete the remote branch in git
- Row Level Locking in the PeopleTools Component Processor
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- 解决:whm搬站出现的mysql error message:Can't find any matching row in the user table
- How can I get the IPv4 address of an interface in linux from C code ?
- How can I run two Django versions in the same server?
- how can i print the data in DataGrid?
- How can i get hold of all of the visible items in my QListView?
- How can I create an Android application in Android Studio that uses the Google Maps Api v2?
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.
- How to Capture the Row Selected on Search Page In a Variable?
- How can I throw an exception without losing the original stack trace information in .NET?
- How to resolve the truncate/drop/delete/join hang issue in ADW
- how websites are perceived by their visitors and the basic ways in which websites can be constructed.