delphistringgrid

Delete row in StringGrid- Delphi


I want to make something like that. I have a list in my StringGrid and i want to delete one row by selecting cell and then clicking the button. Then this list should show again in StringGrid without this row. The biggest problem i have with deleting row, i tried whis one procedure but it only deleted row in StringGrid, not in list, i think.

    procedure DeleteRow(Grid: TStringGrid; ARow: Integer);
var
  i: Integer;
begin
  for i := ARow to Grid.RowCount - 2 do
    Grid.Rows[i].Assign(Grid.Rows[i + 1]);
  Grid.RowCount := Grid.RowCount - 1;
end;

Please someone for help. :)


Solution

  • The selected row can be retrieved StringGrid1.selected and the you can call the following procedure.

    procedure TUtils.DeleteRow(ARowIndex: Integer; AGrid: TStringGrid);
    var
      i, j: Integer;
    begin
      with AGrid do
      begin
        if (ARowIndex = RowCount) then
          RowCount := RowCount - 1
        else
        begin
          for i := ARowIndex to RowCount do
            for j := 0 to ColumnCount do
              Cells[j, i] := Cells[j, i + 1];
    
          RowCount := RowCount - 1;
        end;
      end;
    end;