Recent

Author Topic: grid custom editor  (Read 15813 times)

cd

  • Jr. Member
  • **
  • Posts: 54
grid custom editor
« on: February 23, 2010, 02:42:22 pm »
Assume i want tdateedit to be used as a custom editor for a cell in stringgrid. How to implement this the optimal way?
I tried it this way:
Code: [Select]
procedure TForm1.GridSelectEditor(Sender: TObject; aCol, aRow: Integer;
  var Editor: TWinControl);
var
  r: RECT;
begin
  if arow=2 then
  begin
    Editor:=TDateEdit.Create(Self);
    r:=grid.cellrect(acol,arow);  // 1
    Editor.SetBounds(r.left,r.top,r.right-r.left,r.bottom-r.top);  //2
  end;
end;
But this only creates an editor and places it at the correct cell position - and that's all. To tell the truth i expected lines //1 and //2 to be done automatically by grid - i added them later, because the grid didnt position my editor. And no real editing occurs, cell text doesn't change. Is this OnSelectEditor useful for implementing custom editor?

jesusr

  • Sr. Member
  • ****
  • Posts: 470
Re: grid custom editor
« Reply #1 on: February 23, 2010, 07:22:33 pm »
Do not create the editor in SelectEditor event, create it in other place, for example formCreate, or at design time. See example lazarus/examples/gridexamples/gridcelleditor

davesimplewear

  • Sr. Member
  • ****
  • Posts: 307
    • Davids Freeware
Re: grid custom editor
« Reply #2 on: February 24, 2010, 09:08:07 am »
Check out the code in this unit
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Grids, ComCtrls, StdCtrls, DatePicker;

type

  { TForm1 }

  TForm1 = class(TForm)
    btnTrans: TButton;
    btnCat: TButton;
    cbCat: TComboBox;
    cbTrans: TComboBox;
    dpCal: TDatePicker;
    ilTitle: TImageList;
    lblTrans: TLabel;
    lblCat: TLabel;
    memTrans: TMemo;
    memCat: TMemo;
    sgTrans: TStringGrid;
    procedure btnCatClick(Sender: TObject);
    procedure btnTransClick(Sender: TObject);
    procedure sgTransSelectEditor(Sender: TObject; aCol, aRow: Integer;
      var Editor: TWinControl);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.sgTransSelectEditor(Sender: TObject; aCol, aRow: Integer;
  var Editor: TWinControl);
begin
  if (aCol=0) and (aRow >0) then
   begin
      dpCal.BoundsRect := sgTrans.CellRect(aCol,aRow);
      sgTrans.Cells[sgTrans.Col, sgTrans.Row] :=DateToStr(dpCal.TheDate) ;
      Editor := dpCal;
   end;
   if (aCol=1) and (aRow >0) then
   begin
      cbTrans.BoundsRect := sgTrans.CellRect(aCol,aRow);
      sgTrans.Cells[sgTrans.Col, sgTrans.Row]:=cbTrans.Text;
      Editor := cbTrans;
   end;
    if (aCol=2) and (aRow >0) then
   begin
      cbCat.BoundsRect := sgTrans.CellRect(aCol,aRow);
      sgTrans.Cells[sgTrans.Col, sgTrans.Row]:=cbCat.Text;
      Editor := cbCat;
   end;
end;

procedure TForm1.btnTransClick(Sender: TObject);
begin
  cbTrans.Items := memTrans.Lines;
end;

procedure TForm1.btnCatClick(Sender: TObject);
begin
  cbCat.Items := memCat.Lines;
end;

initialization
  {$I Unit1.lrs}

end.
I used the DatePicker Component for the date editor
Regards
Dave
All things considered insanity seems the best option

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: grid custom editor
« Reply #3 on: March 16, 2010, 10:24:23 am »
I want to use custom editor in DBGrid.

In gridcelleditor example, the following code is used in OnSelectEditor event:
Code: [Select]
ComboBox1.BoundsRect:=StringGrid1.CellRect(aCol,aRow);

In previous post, Davesimplewear uses same thing.

However, it does NOT work in DBGrid. The OnSelectEditor event differs, there are not ACol and ARow parameters. Furthermore, DBGrid's Col and Row properties are protected, so they cannot be used to calculate the editor's position.

It is interesting that this code (without setting the control's position):
Code: [Select]
procedure TForm2.DBGrid1SelectEditor(Sender: TObject; Column: TColumn; var Editor: TWinControl);
begin
  if Column.Field = DBZVDateTimePicker5.Field then
    Editor := DBZVDateTimePicker5;

end;
after first clicking on the appropriate cell moves the editor control (DBZVDateTimePicker5 in this example) to some weird position on the form, but not to where it should be. Then, when I click another cell in the same column, the control completely disappears from screen.

What should I do?
« Last Edit: March 16, 2010, 10:27:19 am by Zoran »

davesimplewear

  • Sr. Member
  • ****
  • Posts: 307
    • Davids Freeware
Re: grid custom editor
« Reply #4 on: March 16, 2010, 11:13:23 pm »
use rxNew package their dbgrid has a date editor in place.

Regards
Dave
All things considered insanity seems the best option

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: grid custom editor
« Reply #5 on: March 17, 2010, 08:06:13 am »
use rxNew package their dbgrid has a date editor in place.

Regards
Dave

Thank you, but it's not the point. I'm looking for a way to use any control as editor in DBGrid cells, the way you described in your earlier post above. Wouldn't it be strange that it can work for StringGrid only, but not possible in DBGrid?

That's besides the facts that I prefere using my control to DateEditor and that rxNew package doesn't compile for me (Laz 0.9.29 rev 23959, Win XP).

jesusr

  • Sr. Member
  • ****
  • Posts: 470
Re: grid custom editor
« Reply #6 on: March 17, 2010, 10:02:18 pm »
Quote
But this only creates an editor and places it at the correct cell position - and that's all. To tell the truth i expected lines //1 and //2 to be done automatically by grid - i added them later, because the grid didnt position my editor

Indeed, I implemented this feature and it worked fine, but then when you try a TDateEdit control as editor you can notice that only part of editor fits within cell boundaries and calendar button remains out, it doesn't look nice.

So I decided that only developer knows well what boundaries are right. So in r24073 I have added a new property SelectedFieldRect that one can use in OnSelectEditor event, this is the equivalent to CellRect(Col,Row) but for the field being handled. In this way one can fit a TDateEdit in a cell with something like this:
Code: [Select]
    ..
    Editor := DateEdit1;
    R := dbGrid1.SelectedFieldRect;
    Dec(R.Right, DateEdit1.ButtonWidth);
    Editor.BoundsRect := R;
    ..

davesimplewear

  • Sr. Member
  • ****
  • Posts: 307
    • Davids Freeware
Re: grid custom editor
« Reply #7 on: March 17, 2010, 10:43:29 pm »
HMMM, I wouldn't mind a data aware string grid, I think that it would add a degree of flexibility that is not in dbGrid.

Regards
Dave
All things considered insanity seems the best option

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: grid custom editor
« Reply #8 on: March 17, 2010, 10:56:41 pm »
So in r24073 I have added a new property SelectedFieldRect that one can use in OnSelectEditor event, this is the equivalent to CellRect(Col,Row) but for the field being handled.

So we'll have it in tomorrow's snapshot! This property was needed, thank you!

fbadriawan

  • New Member
  • *
  • Posts: 15
Re: grid custom editor
« Reply #9 on: December 11, 2010, 05:19:29 am »
So in r24073 I have added a new property SelectedFieldRect that one can use in OnSelectEditor event, this is the equivalent to CellRect(Col,Row) but for the field being handled.

So we'll have it in tomorrow's snapshot! This property was needed, thank you!

Greats, it works perfecly, except the custom editor does not act like native dbgrid's controls on key processing. As we know, if we set AutoAdvance=aaRightDown and we press Enter key it would move the cursor to the next cell/column, where as custom editor does not.

Anayway, I very appreciated for the great works.

Regards.

 

TinyPortal © 2005-2018