I would like to ask if there is another way to alter the cell text without using TextOut()
? Using TextOut()
requires coordinates which I do not want to mingle into. Besides, I just want to change the text and retain everything else, like color, font, alignment, etc.
My purpose is to alter the display of text in different scenarios. For example:
- I may want to show a date field in different formats in different columns, like one column to show in
MM/yyyy
and another column to show inMM/dd/yyyy
. - I may want to display some rows with integer/float datatype with a text that says "too high" or "too low" if the figure is above or below a certain threshold.
- I may want to exchange boolean true/false with some text or numbers as I see fit.
- or perhaps, just blank some cell on certain conditions.
I understand if TDBGrid
is "editable", this would be a tough challenge to do editing. So I intend to use whatever solution in a non-editable grid. And also, I don't want to shift to TStringGrid
as I find TDBGrid
to be easy to work with a dataset.
Btw, I'm using Delphi 7.
For just leaving a certain cell blank on a certain condition, is there something that I could just issue an "exit" to skip the showing of the cell value into the cell itself?
Like:
procedure Tform1.dbgrdDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Column.fieldname = 'total') and (column.field.value=0) then
exit
else
dbgrdsku.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
CodePudding user response:
If you are working with a TDBGrid
and data sources, before opting for changes in the Grid
directly, check if it is useful to do it in the data source (on TFields
).
Using the Datasource
does not spend so many resources painting. Upon reaching that point (painted) the data may already be changed.
You can create the TFields
on TDataset
descendant (TQuery, TADOQuery, TFDQuery,...) and use the OnGetText
event to change the text to paint. Simpler and better.
If you are two fields like this:
With a simple code like this using the OnGetText
of two fields:
procedure TForm3.ADOQuery1DatosGrupoGetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
// too hight for number greater than 5
if (not Sender.IsNull) then
if (Sender.AsInteger > 5) then
Text := 'too hight!'
else
Text := Sender.AsString;
end;
procedure TForm3.ADOQuery1fechaGetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
// Change the date format
if (not Sender.IsNull) then
Text := formatDateTime('MM/yyyy', Sender.AsDateTime)
end;
You got this result: