FireMonkey XE5 - Livebinding - TGrid - Cell text aligment - delphi

I have a Firemonkey Desktop Application for Windows. I have a TGrid which I populate through a visual livebinding TBindSourceDB component. I want some columns to have text aligned to the right, as they are numerical values. I have tried through:
onPainting event
getting the TTextCell control by number of ColumnxRow
typecasting it and setting TextAlignt property to the right
None of those measures align the text to the right. I have tried to set it at runtime, unsuccessfully though, getting the TStyledControl and assigning procedures to the onApplyStyleLookup of the TTextCell.
Any ideas on it? The App runs, but nothing happens. The cell texts are still left aligned.

Use the OnDrawColumnCell Event.
For columns containing text cells, the text layout information for each individual Column is assigned from the TextSettings property of the grid. However, the assignment is performed prior to the event firing.
The best and simplest way is to just directly access the layout for a specific column via a class helper before any drawing takes place.
Set the DefaultDrawing property of the grid to False and paste the following code:
interface
type
TColumnHelper = class helper for FMX.Grid.TColumn
function getTextLayout: TTextLayout;
end;
implementation
{ TColumnHelper }
function TColumnHelper.getTextLayout: TTextLayout;
begin
Result := Self.FDrawLayout;
end;
{ OnDrawColumnCell }
procedure GridDrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
begin
{ change text layout info prior to default drawing }
if Column.Header = 'Numerical Column' then
Column.getTextLayout.HorizontalAlign := TTextAlign.Trailing
else
Column.getTextLayout.HorizontalAlign := TGrid(Sender).TextSettings.HorzAlign;
{ perform default drawing }
TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
end;

Related

How do I correctly size a variable height TListViewItem when using LiveBindings?

I have a TListView which I'm using the OnUpdateObjects event to size the height of the TListViewItem depending no how long a text field is in a DataSet (I'm using LiveBindings). This works pretty good except when I add a new record, the height is initially the design time height of the TListViewItem. When I add an additional item (which is sized incorrectly) the old record gets resized correctly (and so on for adding additional records).
I've based my code around the ListViewVariableHeightItems sample project. The event code looks like the following
procedure TForm8.lvLogUpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
var
itemDetail: TListItemText;
Text: string;
AvailableWidth: Single;
TextHeight : integer;
begin
AvailableWidth := TListView(Sender).Width - TListView(Sender).ItemSpaces.Left
- TListView(Sender).ItemSpaces.Right;
// Find the text drawable which is used to calcualte item size.
itemDetail := TListItemText(AItem.View.FindDrawable('txtDetail'));
Text := itemDetail.Text;
// Calculate item height based on text in the drawable
TextHeight := GetTextHeight(itemDetail, AvailableWidth, Text);
AItem.Height := round(itemDetail.PlaceOffset.Y + TextHeight);
itemDetail.Height := TextHeight;
itemDetail.Width := AvailableWidth;
end;
The GetTextHeight function is straight from the ListViewVariableHeightItems project and seems to work correctly.
I'm after suggestions as to how to get the initial sizing of the TListViewItem to reflect the length of text that txtDetail Drawable contains (or at least should contain from the dataset).
I've only been able to find a rather lame solution to this, append an extra record to the dataset, then cancel it. I'm happy to accept a better solution if anyone can provide one.

Why don't child controls of a TStringGrid work properly?

I am placing checkboxes (TCheckBox) in a string grid (TStringGrid) in the first column. The checkboxes show fine, positioned correctly, and respond to mouse by glowing when hovering over them. When I click them, however, they do not toggle. They react to the click, and highlight, but finally, the actual Checked property does not change. What makes it more puzzling is I don't have any code changing these values once they're there, nor do I even have an OnClick event assigned to these checkboxes. Also, I'm defaulting these checkboxes to be unchecked, but when displayed, they are checked.
The checkboxes are created along with each record which is added to the list, and is referenced inside a record pointer which is assigned to the object in the cell where the checkbox is to be placed.
String grid hack for cell highlighting:
type
THackStringGrid = class(TStringGrid); //used later...
Record containing checkbox:
PImageLink = ^TImageLink;
TImageLink = record
...other stuff...
Checkbox: TCheckbox;
ShowCheckbox: Bool;
end;
Creation/Destruction of checkbox:
function NewImageLink(const AFilename: String): PImageLink;
begin
Result:= New(PImageLink);
...other stuff...
Result.Checkbox:= TCheckbox.Create(nil);
Result.Checkbox.Caption:= '';
end;
procedure DestroyImageLink(AImageLink: PImageLink);
begin
AImageLink.Checkbox.Free;
Dispose(AImageLink);
end;
Adding rows to grid:
//...after clearing grid...
//L = TStringList of original filenames
if L.Count > 0 then
lstFiles.RowCount:= L.Count + 1
else
lstFiles.RowCount:= 2; //in case there are no records
for X := 0 to L.Count - 1 do begin
S:= L[X];
Link:= NewImageLink(S); //also creates checkbox
Link.Checkbox.Parent:= lstFiles;
Link.Checkbox.Visible:= Link.ShowCheckbox;
Link.Checkbox.Checked:= False;
Link.Checkbox.BringToFront;
lstFiles.Objects[0,X+1]:= Pointer(Link);
lstFiles.Cells[1, X+1]:= S;
end;
Grid's OnDrawCell Event Handler:
procedure TfrmMain.lstFilesDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Link: PImageLink;
CR: TRect;
begin
if (ARow > 0) and (ACol = 0) then begin
Link:= PImageLink(lstFiles.Objects[0,ARow]); //Get record pointer
CR:= lstFiles.CellRect(0, ARow); //Get cell rect
Link.Checkbox.Width:= Link.Checkbox.Height;
Link.Checkbox.Left:= CR.Left + (CR.Width div 2) - (Link.Checkbox.Width div 2);
Link.Checkbox.Top:= CR.Top;
if not Link.Checkbox.Visible then begin
lstFiles.Canvas.Brush.Color:= lstFiles.Color;
lstFiles.Canvas.Brush.Style:= bsSolid;
lstFiles.Canvas.Pen.Style:= psClear;
lstFiles.Canvas.FillRect(CR);
if lstFiles.Row = ARow then
THackStringGrid(lstFiles).DrawCellHighlight(CR, State, ACol, ARow);
end;
end;
end;
Here's how it looks when clicking...
What could be causing this? It's definitely not changing the Checked property anywhere in my code. There's some strange behavior coming from the checkboxes themselves when placed in a grid.
EDIT
I did a brief test, I placed a regular TCheckBox on the form. Check/unchecks fine. Then, in my form's OnShow event, I changed the Checkbox's Parent to this grid. This time, I get the same behavior, not toggling when clicked. Therefore, it seems that a TCheckBox doesn't react properly when it has another control as its parent. How to overcome this?
TStringGrid's WMCommand handler doesn't allow children controls to handle messages (except for InplaceEdit).
So you can use e.g. an interposed class (based on code by Peter Below) or draw controls by hands, as some people have adviced. Here is the code of the interposed class:
uses
Grids;
type
TStringGrid = class(Grids.TStringGrid)
private
procedure WMCommand(var AMessage: TWMCommand); message WM_COMMAND;
end;
implementation
procedure TStringGrid.WMCommand(var AMessage: TWMCommand);
begin
if EditorMode and (AMessage.Ctl = InplaceEditor.Handle) then
inherited
else
if AMessage.Ctl <> 0 then
begin
AMessage.Result := SendMessage(AMessage.Ctl, CN_COMMAND,
TMessage(AMessage).WParam, TMessage(AMessage).LParam);
end;
end;
In Delphi7 at least I do this:
You need to draw a checkbox on the cell, and keep it in sync with an array of boolean (here fChecked[]) that indicates the state of the checkbox in each row. Then, in the DrawCell part of the TStringGrid:
var
cbstate: integer;
begin
...
if fChecked[Arow] then cbState:=DFCS_CHECKED else cbState:=DFCS_BUTTONCHECK;
DrawFrameControl(StringGrid.canvas.handle, Rect, DFC_BUTTON, cbState);
...
end;
To get the checkbox to respond to the space-bar, use the KeyDown event, and force a repaint:
if (Key = VK_SPACE) And (col=ColWithCheckBox) then begin
fChecked[row]:=not fChecked[row];
StringGrid.Invalidate;
key:=0;
end;
A similar approach is needed for the OnClick method.
Can u use VirtualTreeView in toReportMode (TListView emulating) mode instead of grid ?
Can u use TDBGrid over some in-memory table like NexusDB or TClientDataSet ?
Ugly approach would be presenting checkbox like a letter with a custom font - like WinDings or http://fortawesome.github.com/Font-Awesome
This latter is most easy to implement, yet most ugly to see and most inflexible to maintain - business logic gets intermixed into VCL event handlers

What is the rect of a clicked Title on a TDBGrid in Delphi?

I'd like to identify the screen coordinates of the Title cell which was clicked on the TDBGrid event TitleClick(Column: TColumn).
I can use the ColWidths property (exposed via a TDBGrid = class(DBGrids.TDBGrid) type declaration) but I am having difficulty in determining if the columns have been re-ordered by the user, combined with horizontal scrolling of the TDBGrid. I'd also like to keep track of where this Column is during subsequent moves and resizings, noting also that it's possible this column can be scrolled off the grid.
I've spent a long while on this problem and I am rather good at Delphi so this is not an idle question.
Using the trick in How do I get screen coordinates of the DBGrid cell, I wrote:
type
...
THackedGrid = class(TDBGrid);
...
implementation
...
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
currRow : Integer;
rect : TRect;
begin
currRow := THackedGrid(DBGrid1).Row;
rect := THackedGrid(DBGrid1).CellRect(Column.Index+1,currRow);
end;
Is this what you want? The coordinate in rect is relative to the grid.
I started working on a very similar grid yesterday at work. I am overlaying a control on the grid fixed row as you mentioned, activating it on a right click. This is what I doing so far, then setting the filter on my dataset. I have issues however when using multiselect on the combo. I would be very interested in seeing what you have accomplished since the last post.
procedure Tf_well.dbWellGridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var cell : TGridCoord;
begin
if Button = mbRight then
begin
Cell := dbWellGrid.MouseCoord(X, Y);
// showmessage(dbWellGrid.Columns[Cell.X-1].DisplayName);
case Cell.X-1 of
0: begin
fieldComboWellName.Visible:=True;
fieldComboWellName.DroppedDown:=True;
fieldComboWellName.SetFocus;
end;

Delphi TDBGrid How to change selected color when style is gdsGradient

I'm just trying to use delphi XE, before that I've been a big fan of Delphi7.
I see the new dbgrid allows to use themed and gradient styles.
I'm using gradient and set rowselect, it has a property for gradient-start and -end for the column header.
But where is the property to set the selected color ?
It's strange because the color doesn't match, selected color is always a blue gradient.
I can do it with customdraw, I just want to know if there is anyway to change it without custom drawing.
The selected color comes from the OS.
There it's coded as clHighlight.
You cannot change it as such, but you can subclass the dbgrid and override the DrawCell method.
Or even easier add a onDrawCell eventhandler.
procedure TForm1.DBGrid1DrawCell(Sender: TObject, const Rect: TRect; Field: TField; State: TGridDrawState);
var
index: Integer;
begin
if not(gdSelected in State) then DefaultDrawCell(Rect, Field, State)
else begin
index := ARow * DBGrid1.ColCount + ACol;
DBGrid1.Canvas.Brush.Color := clYellow; <<-- some color
DBGrid1.Canvas.FillRect(Rect);
if (gdFocused in State) then begin
DBGrid1.Canvas.DrawFocusRect(Rect);
end;
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,index, True);
end;

QuantumGrid VCL - How to change the text of a Hyperlink column in the cell click event?

Using: Delphi XE, Devexpress VCL.
In the cell click event, I am trying to change the value of a cell in a hyperlink column in Devexpress's QuantumGrid VCL control. The column is a custom column and is not bound to the dataset.
The hyperlink column's properties are set as per:
Editing := False;
ReadOnly := True;
SingleClick := True;
The following code (grdReprint is the grid's DBTableView, and, grdReprintColumn2 is the Hyperlink column) is ineffective:
procedure TfReceiptList.grdReprintCellClick(Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: boolean);
var
v: integer;
c: integer;
begin
if ACellViewInfo.Item = grdReprintColumn1 then
begin
v := datamod.uspRECEIPT_LSTRECEIPTID.AsInteger;
fMain.PrintReceipt(v);
end
else if ACellViewInfo.Item = grdReprintColumn2 then
begin
(* This code is ineffective because the cell contents do not change *)
if ACellViewInfo.Text = 'Void' then
grdReprint.DataController.SetEditValue(grdReprintColumn2.Index, 'Unvoid', evsValue)
else
grdReprint.DataController.SetEditValue(grdReprintColumn2.Index, 'Void', evsValue);
end;
end;
If the above isn't the proper way to change the text in the cell, then other ideas are welcome.
TIA.
When the SingleClick property in the hyperlink control is set to TRUE the GridViews CellClick event is not called.
(I may be able to further help if I could understand why you a using a hyperlink control for what looks like just text. See my coments below your question.)
EDIT: This answer is incorrect if the gridViews Editing property is False as OP indicated. It is does describe the behavior if Editing is True FWIW.

Resources