Monday, September 12, 2011

Delete Row or Column in a TStringGrid

Create a Form and put an StringGrid on it
add this procedure on your form

procedure RemoveRowColStringGrid(StringGrid: TStringGrid;
  RowCol: boolean; Which: integer);
var
  i: integer;
begin
  if RowCol= True then
  begin
    for i := StringGrid.Row to StringGrid.RowCount - 1 do
    begin
      StringGrid.Rows[i] := StringGrid.Rows[i + 1];
    end;
    StringGrid.RowCount := StringGrid.RowCount - 1;
  end
  else
  begin
    for i := StringGrid.Col to StringGrid.ColCount - 1 do
    begin
      StringGrid.Cols[i] := StringGrid.Cols[i + 1];
    end;
    StringGrid.ColCount := StringGrid.ColCount - 1;
  end;
end;
In this example we will use the event OnKeyDown of StringGrid
procedure Form1.StringGrid1.KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_DELETE then //if key is delete
begin
  RemoveRowColStringGrid(
"Your StringGrid",
"True to remove row, False to remove col",
"Index of the row you want to remove");
end;
end;

6 comments:

  1. Thanks for the tips!
    Check me out =) alphabetalife.blogspot.com

    ReplyDelete
  2. Great, this will definitely come in handy..

    ReplyDelete
  3. Interesting read! I caught this post from your title and glad that I found it! Keep up the great work with your blog and I look forward to reading your next post!

    ReplyDelete
  4. nice article ... thanks http://zonateknologi.id

    ReplyDelete
  5. Muito obrigado pelos códigos!! :)

    ReplyDelete