Events
TDataGridEh Published Events
TDataGridEh.OnCanUserSelectRow
property OnCanUserSelectRow: TDataGridCanUserSelectCurrentRowEventEh;published
The event occurs when the user attempts to select a specific record in the grid. In the event handler, you can prevent the user from selecting a specific record based on certain conditions.
The event is of type TDataGridCanUserSelectCurrentRowEventEh and is defined as follows.:
TDataGridCanUserSelectCurrentRowEventEh =
procedure(Sender: TObject; Params: TDataGridCanSelectRowParamsEh)
of object;Sender: TObjectThe Sender parameter is passed the grid that caused this event.
Params: TDataGridCanSelectRowParamsEhThe Params parameter contains additional properties that can be used to determine whether the user can select a particular record.
The TDataGridCanSelectRowParamsEh type has the following properties and methods:
property Grid: TDataGridEh- the grid the event belongs to.property Row: TDataGridRowEh- The row the user is trying to select.property CanSelectRow: Boolean- Set this property to True or False depending on whether the user is allowed to select a record.property Handled: Boolean- Set this property to True if you have fully handled the event and the grid should not call the default method that determines whether the record can be selected.
The following example shows an event handler that prevents a record from being selected in the grid if the record contains the value 207 in the ColOrderNo column:
unit UnitDataGridEventsTester;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, MemTableDataEh,
Data.DB, EhLibFmx.Api,
FMX.Controls.Presentation, FMX.StdCtrls, EhLibFmx.DataGrids,
EhLibFmx.DataAxisGrid.FieldBars,
System.Rtti, EhLibUtils,
EhLibFmx.DataGrid.Columns, EhLibFmx.ToolControls, EhLibFmx.Grids,
EhLibFmx.DataAxisGrids, EhLibFmx.CustomDataGrids,
MemTableEh;
type
TFormDataGridEventsTester = class(TForm)
MemTableEh1: TMemTableEh;
DataGridEh1: TDataGridEh;
ColOrderNo: TDataGridStringColumnEh;
ColTaxRate: TDataGridStringColumnEh;
ColLay: TDataGridLayoutColumnEh;
procedure DataGridEh1CanUserSelectRow(Sender: TObject;
Params: TDataGridCanSelectRowParamsEh);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormDataGridEventsTester: TFormDataGridEventsTester;
implementation
{$R *.fmx}
procedure TFormDataGridEventsTester.DataGridEh1CanUserSelectRow(
Sender: TObject; Params: TDataGridCanSelectRowParamsEh);
var
Val: TValue;
begin
Val := ColOrderNo.GetRowValue(Params.Row);
if SameValue(Val, 207) then
begin
Params.CanSelectRow := False;
Params.Handled := True;
end;
end;
end.TDataGridEh.OnColEnter
property OnColEnter: TDataGridEventEh;published
The event is called after moving the current cell from one column to another. You can get a reference to the current column using the TDataGridEh.CurrentColumn property or the index to the current column - TDataGridEh.CurrentColIndex.
TDataGridEh.OnColExit
property OnColExit: TDataGridEventEh;published
The event is called before the current cell is moved from one column to another. You can get a reference to the current column using the TDataGridEh.CurrentColumn property or the index to the current column - TDataGridEh.CurrentColIndex.
TDataGridEh.OnColumnWidthChanged
property OnColumnWidthChanged: TDataGridColumnWidthChangedEventEh;published
The event is fired after the width of any data column is changed.
TDataGridEh.OnCurrentChange
property OnCurrentChange: TDataGridEventEh;published
The event is fired after the following changes in the grid:
- The original data list has changed.
- The content of the current record has changed.
- The position of the current record has changed.
- The position of the current column has changed.
In the following example, the event handler writes the value of the current cell to the Text property of the TLabel control whenever there is a change in the grid or whenever the position of the current cell changes.
procedure TFormDataGridEventsTester.DataGridEh1CurrentChange(
Sender: TObject;
Params: TDataGridEventParamsEh);
var
Grid: TDataGridEh;
Val: TValue;
begin
Grid := TDataGridEh(Sender);
Label2.Text := 'RowIndex = ' + Grid.CurrentRowIndex.ToString + '; CellValue = ';
if (Grid.CurrentRow <> nil) and (Grid.CurrentColumn <> nil) then
begin
Val := Grid.CurrentColumn.GetRowValue(Grid.CurrentRow);
Label2.Text := Label2.Text + Grid.CurrentColumn.GetDisplayText(Val);
end else
Label2.Text := Label2.Text + '?';
end;TDataGridEh.OnDataCellGetDisplayText
property OnDataCellGetDisplayText: TDataGridDataCellGetDisplayTextEventEh;published
The event is called when the cell value is converted to a value for display on the screen in the grid cell. You can add a new value in the Params.DisplayText parameter. If you handle the event, also set the Params.Handled := True property so that the grid does not call the default event handler. In the event handler, you can also get the default value using the Params.GetDefaultDisplayText() method.
In this example the event handler add 'OrderNo = ' text to the DisplayText parameter value for the static column ColOrderNo.
procedure TFormDataGridEventsTester.DataGridEh1DataCellGetDisplayText(
Sender: TObject;
Params: TDataGridDataCellGetDisplayTextParamsEh);
begin
if Params.Value.IsEmpty then Exit;
if Params.Column = ColOrderNo then
begin
Params.DisplayText := 'OrderNo = ' + Params.GetDefaultDisplayText();
Params.Handled := True;
end;
end;TDataGridEh.OnDataCellGetStyleParams
property OnDataCellGetStyleParams: TDataGridDataCellGetStyleParamsEventEh;published
The event is called when the cell display parameters are set. In the event handler, you can set the following cell parameters: Fill, Font, FontColor, HorzAlign, Padding, VertAlign.
In the following event handler, the program code accesses the source data store of the table record Params.Row.SourceRowLink and the grid data store property Params.Grid.TableView to get a reference to the 'CustNo' field from the grid table data store.
If the field value < 10, then a special color and cell font style are set for all columns of the grid for the current record:
procedure TFormDataGridEventsTester.DataGridEh1DataCellGetStyleParams(
Sender: TObject;
Params: TDataGridDataCellStyleParamsEh);
var
CustNo: TValue;
TableRowLink: TTableRowLinkEh;
FieldLink: TTableFieldLinkEh;
begin
if Params.Row = nil then Exit;
TableRowLink := Params.Row.SourceRowLink;
FieldLink := Params.Grid.TableView.Fields.FindField('CustNo');
CustNo := TableRowLink.FieldValue[FieldLink];
if (ValueToInteger(CustNo) < 10) then
begin
Params.FontColor := TAlphaColorRec.Cadetblue;
Params.Font.Style := [TFontStyle.fsBold];
end;
end;TDataGridEh.OnDataCellGetValue
property OnDataCellGetValue: TDataGridDataCellGetValueEventEh;published
The event is called when the grid accesses the table storage to get the value for the required row and column.
You can assign a value to the Params.Value property to replace the requested value.
After assigning, do not forget to indicate that the event is handled: Params.Handled := True.
In the following example, when the grid accesses data for the ColTestCheckBox column, the code replaces the default value with a value calculated based on the field value in the ColCustNo column.
procedure TFormDataGridEventsTester.DataGridEh1DataCellGetValue(Sender: TObject;
Params: TDataGridDataCellGetValueParamsEh);
var
Val: TValue;
begin
if Params.Row = nil then Exit;
if Params.Column = ColTestCheckBox then
begin
Val := ColCustNo.GetRowValue(Params.Row);
if ValueToInteger(Val) < 10 then
Params.Value := True
else
Params.Value := False;
Params.Handled := True;
end;
end;TDataGridEh.OnDataCellKeyDown
property OnDataCellKeyDown: TDataGridDataCellKeyDownEventEh;published
The event has not yet been described.
TDataGridEh.OnDataCellMouseClick
property OnDataCellMouseClick: TDataGridDataCellMouseButtonEventEh;published
The event has not yet been described.
TDataGridEh.OnDataCellMouseDown
property OnDataCellMouseDown: TDataGridDataCellMouseButtonEventEh;published
The event has not yet been described.
TDataGridEh.OnDataCellSetValue
property OnDataCellSetValue: TDataGridDataCellSetValueEventEh;published
The event is called when a changed value is written from the cell editor to the data storage.
In the event handler, you can write the value yourself to the location in the storage where you think it is necessary or replace the value in the Params.Value property or check the value, raise an exception, report an incorrect value entered.
In the following example, the handler code checks that the entered value was in the ColOrderNo column and if so, removes the text 'OrderNo = ' from the entered value. In this example, we do not set the Params.Handled := True value to indicate that the changed value should be written to the data store by the default method:
procedure TFormDataGridEventsTester.DataGridEh1DataCellSetValue(
Sender: TObject;
Params: TDataGridDataCellSetValueParamsEh);
var
StrValue: String;
begin
if Params.Value.IsEmpty then Exit;
if Params.Column = ColOrderNo then
begin
StrValue := ValueToString(Params.Value);
Params.Value :=
StringReplace(StrValue, 'OrderNo = ', '', [rfIgnoreCase]);
// Params.Handled := True;
end;
end;TDataGridEh.OnFilterRow
property OnFilterRow: TDataGridFilterRowParamsEventEh;published
The event is called for each record of the grid's source data to determine whether the record is visible in the grid.
If you set Params.Accept := False in the event, the record will not be visible in the grid.
In the following example, the event handler filters out all records whose Terms field value is not equal to 'FOB'.
procedure TFormDataGridEventsTester.DataGridEh1FilterRow(
Sender: TObject;
Params: TDataGridFilterRowParamsEh);
var
TermsValue: TValue;
TableRowLink: TTableRowLinkEh;
FieldLink: TTableFieldLinkEh;
begin
if Params.Row = nil then Exit;
TableRowLink := Params.Row.SourceRowLink;
FieldLink := Params.Grid.TableView.Fields.FindField('Terms');
TermsValue := TableRowLink.FieldValue[FieldLink];
if (Params.Accept = True) and
(SameValue(TermsValue, 'FOB') = False) then
begin
Params.Accept := False;
end;
end;TDataGridEh.OnGetDataCellManager
property OnGetDataCellManager: TDataGridGetDataCellManagerEventEh;published
The event is called to request the CellManager object. The event is called for each grid cell during each rendering operation. The CellManager object can create cells of the same type and set the cell properties to display the correct information. The CellManager object also has virtual methods when certain interactions occur on a grid cell such as mouse movement, mouse clicks, context menu display, and other events.
In the following example, the event handler checks the type of the source object for the current grid record and, depending on the type, returns a specific CellManager that will be responsible for generating grid cells.
procedure TfrContacts.DataGridEh1GetDataCellManager1(
Sender: TObject;
Params: TDataGridGetDataCellManagerParamsEh);
var
SrcItemObject: TPersistent;
begin
if Params.Row = nil then Exit;
SrcItemObject :=
TObject(TTypedListItemLinkEh(Params.Row.SourceRowLink).SourceItem) as
TPersistent;
if SrcItemObject is TContactListItem then
begin
Params.CellManager := ListDataItemCellMan;
end else
begin
Params.CellManager := ListCaptionCellMan;
end;
end;The following prerequisites must be met to implement this example:
CellManagers must be created in advance.
The grid must work with source data of the TList type. Only in this case we can get the list element links in the event using the code
SrcItemObject := TObject(TTypedListItemLinkEh(Params.Row.SourceRowLink).SourceItem) as TPersistent;
.
The TContactListItem class must be declared and implemented in the program code.
You can see a more detailed example code in the source code of the EhLib.Fmx.MainDemo.dproj project in the DemoFrame.Contacts.pas file.
TDataGridEh.OnSelectionChanged
property OnSelectionChanged: TDataGridEventEh;published
The event is called when the selected area in the grid changes. Typically, this event is used to calculate information about the selected area of the grid.
In the following example, the event handler requests information about the number of selected rows and writes it to the Text property of the TLabel control.
procedure TFormDataGridEventsTester.DataGridEh1SelectionChanged(
Sender: TObject;
Params: TDataGridEventParamsEh);
begin
if Params.Grid.SelectedRows.Count = 0 then
Label1.Text := '<No rows selected>'
else if Params.Grid.SelectedRows.Count = 1 then
Label1.Text := ' The grid have 1 row selected'
else
Label1.Text := 'The grid has ' +
Params.Grid.SelectedRows.Count.ToString +
' rows selected';
end;