Jump to content
Sign in to follow this  
sw4all

DBGrid DrawColumnCell

Recommended Posts

Hello to everybody.
I need to add a button to each cell and use one click function.
Unfortunately I get an error Access violation at address ...

Thank you all.

Daniel

public
    { Public declarations }
    procedure btnChangeSClick(Sender: TObject);
   ...
   ...
   
procedure TfrmMain.btnChangeSkupinaClick(Sender: TObject);
begin
  frmSelectS.gridName := 'TEST';
  frmSelectS.ShowModal;
end;

procedure TfrmMain.grid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Field: TField;
  fixRect : TRect;
  btnChangeS: TButton;
begin

  Field := Column.Field;

  if (Column.Index = 0) then
  begin
    try
      btnChangeS.Create(grid1);
      btnChangeS.Parent := grid1;
      btnChangeS.Visible := True;
      btnChangeS.Caption := '...';
      btnChangeS.Width := 25;
      btnChangeS.Height := (Rect.Bottom - Rect.Top) - 2;
      btnChangeS.Left := (Rect.Left + Rect.Width) - (btnChangeS.Width +1);
      btnChangeS.Top := Rect.Top + 1;
      btnChangeS.OnClick := btnChangeSClick;
      grid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    except
      On E: exception do
      begin
        ShowMessage(E.Message);
      end;
    end;
  end;

end;

 

Edited by sw4all

Share this post


Link to post
2 minutes ago, Pat Foley said:

btnChangeS := TButton.Create(grid1);

Yes.
Thank you so much for your quick help.
Everything is working.

Share this post


Link to post
46 minutes ago, sw4all said:

I see one more error and that is that the procedure still creates more and more buttons.

It's not an error, it's how you've coded to create a new button in every paint event. Instead of that I would use the inbuild feature of the dbgrid.

 

On you column set ButtonStyle to cbsEllipsis then you can place your in the OnEditButtonClick event of the grid.

 

image.png.077896a413aa7cad99751904db6ee967.png

  • Like 1

Share this post


Link to post
26 minutes ago, Lajos Juhász said:

It's not an error, it's how you've coded to create a new button in every paint event. Instead of that I would use the inbuild feature of the dbgrid.

 

On you column set ButtonStyle to cbsEllipsis then you can place your in the OnEditButtonClick event of the grid.

 

image.png.077896a413aa7cad99751904db6ee967.png

Yes thank you.
The problem is that dgEditing will not be enabled, so the button will not appear.

Oh, just set DBGrid readOnly: = True;

Edited by sw4all

Share this post


Link to post

Is there any way for the button to still be visible, not only when a cell is selected?

So that the user can see which cells he can edit.

Edited by sw4all

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×