[英]How to use onDrawColumnCell to change cell text without using textout on Delphi?
I would like to ask if there is another way to alter the cell text without using TextOut()
?我想问一下是否有另一种方法可以在不使用
TextOut()
的情况下更改单元格文本? Using TextOut()
requires coordinates which I do not want to mingle into.使用
TextOut()
需要我不想混入的坐标。 Besides, I just want to change the text and retain everything else, like color, font, alignment, etc.此外,我只想更改文本并保留其他所有内容,如颜色、字体、alignment 等。
My purpose is to alter the display of text in different scenarios.我的目的是改变不同场景中文本的显示。 For example:
例如:
MM/yyyy
and another column to show in MM/dd/yyyy
.MM/yyyy
中,另一列显示在MM/dd/yyyy
中。 I understand if TDBGrid
is "editable", this would be a tough challenge to do editing.我知道如果
TDBGrid
是“可编辑的”,这将是进行编辑的一项艰巨挑战。 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.而且,我不想转向
TStringGrid
,因为我发现TDBGrid
很容易处理数据集。
Btw, I'm using Delphi 7.顺便说一句,我正在使用 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 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
).如果您正在使用
TDBGrid
和数据源,在直接选择在Grid
中进行更改之前,请检查在数据源(在TFields
上)中进行更改是否有用。
Using the Datasource
does not spend so many resources painting.使用
Datasource
不会花费那么多资源进行绘画。 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.您可以在
TDataset
后代(TQuery、TADOQuery、TFDQuery 等)上创建TFields
,并使用OnGetText
事件更改要绘制的文本。 Simpler and better.更简单更好。
If you are two fields like this:如果你是这样的两个字段:
With a simple code like this using the OnGetText
of two fields:使用像这样使用两个字段的
OnGetText
的简单代码:
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:你得到了这个结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.