user13898928
user13898928

Reputation:

Is there a way to call a StringGrid OnCellDraw during runtime

I have a program that tracks the days during the year which are booked. In order to display this I have a StringGrid which I use Colors to display the days booked. The days booked are stored in ar2Booking which is a 2D array which contains the days and months respectively.

procedure TfrmClient.stgYearPlan1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  k, iMonth, iDay : Integer;
begin
for k := 1 to 31 do
  stgYearPlan1.Cells[k,0] := IntToStr(k);

for k := 1 to 12 do
  stgYearPlan1.Cells[0,k] := ShortMonthNames[k];

for iDay := 1 to 31 do
 for iMonth := 1 to 12 do
 begin
      if ar2Booking[iDay,iMonth] = 'Y' then
      begin
        if (ACol = iDay) and (ARow = iMonth) then
        begin
          stgYearPlan1.Canvas.Brush.Color := clBlack;
          stgYearPlan1.Canvas.FillRect(Rect);
          stgYearPlan1.Canvas.TextOut(Rect.Left,Rect.Top,stgYearPlan1.Cells[ACol, ARow]);
        end;
      end;

      if ar2Booking[iDay,iMonth] = 'D' then
      begin
        if (ACol = iDay) and (ARow = iMonth) then
        begin
          stgYearPlan1.Canvas.Brush.Color := clSilver;
          stgYearPlan1.Canvas.FillRect(Rect);
          stgYearPlan1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,stgYearPlan1.Cells[ACol, ARow]);
        end;
      end;
 end;
end;

I then want to click a button during runtime which allows a user to book a date. I would then like the date they select to reflect in the StringGrid. If I update the array how would I be able to run the OnCellDraw again in order to reflect the new booked dates?

Thanks

Upvotes: 1

Views: 497

Answers (2)

user13898928
user13898928

Reputation:

I discovered after a friend showed me, the StringGrid.Redraw procedure accomplishes what I need. Thanks everyone

Upvotes: 1

Brian
Brian

Reputation: 7289

Generally you would invalidate part of the control causing it to be redrawn with the next windows paint message. The methods of a TStringGrid to do this are protected so you need to use a cracker class to access them.

// -- add to the type section 
type
  TStringGridCracker = class(TStringGrid);

procedure TForm1.Button1Click(Sender: TObject);
begin
   TStringGridCracker(StringGrid1).InvalidateCell(1,2);
end;

Upvotes: 1

Related Questions