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/yyyyand 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;
If you are working with a
TDBGridand data sources, before opting for changes in theGriddirectly, check if it is useful to do it in the data source (onTFields).Using the
Datasourcedoes not spend so many resources painting. Upon reaching that point (painted) the data may already be changed.You can create the
TFieldsonTDatasetdescendant (TQuery, TADOQuery, TFDQuery,...) and use theOnGetTextevent to change the text to paint. Simpler and better.If you are two fields like this:
With a simple code like this using the
OnGetTextof two fields:You got this result: