简体   繁体   English

如何在 Delphi 上使用 onDrawColumnCell 更改单元格文本而不使用 textout?

[英]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:例如:

  • 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 in MM/dd/yyyy .我可能想在不同的列中以不同的格式显示日期字段,例如一列显示在MM/yyyy中,另一列显示在MM/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.我可能想用我认为合适的一些文本或数字交换 boolean true/false。
  • or perhaps, just blank some cell on certain conditions.或者,也许只是在某些条件下空白一些单元格。

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.

相关问题 如何更改TCanvas(delphi)的textOut的颜色? - How can I change the color of textOut of TCanvas (delphi)? 在DBGrid中使用Canvas.TextOut - Delphi XE 3 - Using Canvas.TextOut in DBGrid - Delphi XE 3 Delphi 6:如何通过TextOut()方法显示大尺寸高质量文本? - Delphi 6 : How can I display large size high quality Text via the TextOut() method? 如何更改TCanvas的textOut的高度? - How can I change the height of textOut of TCanvas? Delphi 6:如何使用具有Alpha通道的TextOut创建位图? - Delphi 6 : How to create a Bitmap with TextOut that has an Alpha channel? 如何使用Delphi XE7更改stringgrid特定单元格的颜色 - How to change the colour of the stringgrid particular cell using Delphi XE7 如何使用delphi自动化在MSWOrd中的表格单元格中选择文本? - How to select text in a cell of a table in MSWOrd using delphi automation? 使用RightToLeft BidiMode的Delphi画布文本输出 - Delphi Canvas Textout with RightToLeft BidiMode 如何在Delphi中更改TCalendar组件中的单元格颜色? - How to change cell color in a TCalendar Component in Delphi? 如何在Delphi中使用DirectWrite在列表框的画布上使用绘制文本? - How to use draw text on the canvas of listbox using DirectWrite in Delphi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM