• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3803
  • Last Modified:

BeginUpdate for TStringGrid

I have a string grid and it flashes whilst data being written to the cells.  I would like to implement something like BeginUpdate and EndUpdate but these do not seem to be implemented in the TStringGrid - anybody suggest how this could be done?

Chris Bray.
0
chrisbray
Asked:
chrisbray
  • 2
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
You could try locking the stringlists that make up the columns, eg:

procedure LockGrid(Grid: TStringGrid);
var  dwIndex:       Integer;
begin

  // Walk the columns and lock the string list for each
  for dwIndex:=0 to Pred(Grid.ColCount) do
  begin
     Grid.Cols[dwIndex].BeginUpdate;
  end;

end;

procedure UnLockGrid(Grid: TStringGrid);
var  dwIndex:       Integer;
begin

  // Walk the columns and unlock the string list for each
  for dwIndex:=0 to Pred(Grid.ColCount) do
  begin
     Grid.Cols[dwIndex].EndUpdate;
  end;

end;

// Example usage
procedure TForm1.Button1Click(Sender: TObject);
var  dwIndex:       Integer;
     dwCols:        Integer;

begin

  LockGrid(StringGrid1);
  try
     for dwIndex:=0 to Pred(StringGrid1.RowCount) do
     begin
        for dwCols:=0 to Pred(StringGrid1.ColCount) do
        begin
           StringGrid1.Cells[dwCols, dwIndex]:=Format('%d, %d', [dwCols, dwIndex]);
        end;
     end;
  finally
     UnLockGrid(StringGrid1);
  end;

end;


Regards,
Russell
0
 
ZhaawZSoftware DeveloperCommented:
another way is sending wm_setredraw message to disable redrawing while you're updating data and then sending it again to enable redrawing.

var
  n, w, h : integer;
begin
with StringGrid1 do begin
  SendMessage(Handle, WM_SETREDRAW, 0, 0);
  for h := 0 to RowCount - 1 do begin
    for w := 0 to ColCount - 1 do begin
      Cells[w, h] := format('%d x %d', [w+1, h+1]);
    end;
  end;
  SendMessage(Handle, WM_SETREDRAW, 1, 0);
  Repaint;
end;
end;
0
 
chrisbrayAuthor Commented:
Hi Guys,

Thanks for your answers.  ZhaawZ gave the quickest and most effective answer, so I have given him the points.  Excellent stuff!

Chris Bray.
0
 
ZhaawZSoftware DeveloperCommented:
glad to help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now