Lucho
Lucho

Reputation: 51

How works the event DrawColumnCell from delphi 7?

I'm using this event (DrawOnColumnCell) for put a datetimepicker in a DBGrid, more precice in a field called "fechaprobablealta". This grid have a client datasource that have a providerDataSet and this take data from a SQLDataSet. I can draw a datetimepicker good until that i tried update the clientdataset. Here a have a error from "Access violation at address" that i couldn't resolve. Here i puts the code:

procedure TfmForm.gdGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if ckCasosAlta.Checked then
  begin
    dtFechaProbableAlta.Visible:=False;
    if cDataSet.FieldByName('fechaalta').Value <> null then
    begin
      gdGrid.Canvas.Brush.Color:=clLime;
      gdGrid.DefaultDrawColumnCell
      (Rect, DataCol, Column, State);
    end;
  end
  else
  begin
    if (gdSelected in State) then
    begin
      if (Column.Field.FieldName = 'fechaprobablealta') then
      begin
        with dtFechaProbableAlta do
        begin
          Left:= Rect.Left + gdGrid.Left+1;
          Top:= Rect.Top + gdGrid.Top+1;
          Width:= Rect.Right - Rect.Left+ 2;
          Height:= Rect.Bottom - Rect.Top +2;
          Date:= Column.Field.AsDateTime;
          Visible:=True;
        end;
      end;
    end;
  end;
end;

enter image description here

When chekcbox is checked:

here the metod when press the checkbox:

procedure TfmForm.ckCasosAltaClick(Sender: TObject);
begin
  if ckCasosAlta.Checked then
  begin
    chkCasosProbableAlta.Checked:=False;
    cDataSet.Close; //ClientDataSet
    qDataSet.CommandText:=  //SQLDataSet
                            //SQL QUERY
                            ;           
  end
  else
  begin
        cDataSet.Close; //ClientDataSet
    qDataSet.CommandText:=  //SQLDataSet
                            //SQL QUERY
                            ;    
  end;
  cDataSet.Open;
  cDataSet.Refresh;
end;

This works good in begin and too when press the checkbox "ckCasosAlta", (the client dataset is closed, change the query, open again and refresh) where hide the datetimepicker and paint the cell called "fechaalta" of green. The problem come when come back to press the checkbox (cheked:= false). The client data set do the same proces and stay like at begin (same sql query). Here have the problem. I have a "Acces violation at adress..".

enter image description here

So i need to know how works the event DrawColumnCell from a dbgrid to be able to debug this problem or know the problem in this case

Upvotes: 0

Views: 728

Answers (1)

AlexAnt
AlexAnt

Reputation: 56

The error might be here:

Date:= Column.Field.AsDateTime;

In case the dataset is closed, a column will be nil, and it can produce an access violation.

Upvotes: 1

Related Questions