Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How can I delete the row in TStringGrid component Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
09-Jun-03
Category
VCL-General
Language
Delphi 2.x
Views
169
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Mike Shkolnik

How can I delete the row in TStringGrid component?

Answer:

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: 
1   
2   procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
3   var
4     i, j: Integer;
5   begin
6     with yourStringGrid do
7     begin
8       for i := ARow to RowCount - 2 do
9         for j := 0 to ColCount - 1 do
10          Cells[j, i] := Cells[j, i + 1];
11      RowCount := RowCount - 1
12    end;
13  end;


2. the modificated #1: 
14  
15  procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
16  var
17    i: Integer;
18  begin
19    with yourStringGrid do
20    begin
21      for i := ARow to RowCount - 2 do
22        Rows[i].Assign(Rows[i + 1]);
23      RowCount := RowCount - 1
24    end;
25  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. 

26  type
27    THackStringGrid = class(TStringGrid);
28  
29  procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
30  begin
31    with THackStringGrid(yourStringGrid) do
32      DeleteRow(ARow);
33  end;


Personally I use the third method but the first and second are more visual.

Also you should clear the Row after moving the data to the row above.  If not you 
will get the old date back when you add a Row. 

34  if StringGrid.RowCount > 2 then
35  begin
36    if StringGrid.Selection.Top <> (StringGrid.RowCount - 1) then
37    begin
38      for iRow := StringGrid.Selection.Top to (StringGrid.RowCount - 2) do
39      begin
40        for iCol := 0 to (StringGrid.ColCount - 1) do
41        begin
42          StringGrid.Cells[iCol, iRow] := StringGrid.Cells[iCol, iRow + 1];
43        end;
44      end;
45    end;
46    StringGrid.Rows[StringGrid.RowCount - 1].Clear;
47    StringGrid.RowCount := StringGrid.RowCount - 1;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC