简体   繁体   English

DBGrid中的千位分隔符

[英]Thousand Separator in DBGrid

I'm using MySQL and Delphi XE4 to build an application. 我正在使用MySQL和Delphi XE4来构建应用程序。 There are big numbers in Database and I used FORMAT function in my queries to apply Thousand Separator! 数据库中有很多数字,我在查询中使用FORMAT函数来应用千位分隔符!

SELECT Field1,FORMAT((Field2 * Coef), 0) AS blabla FROM MyTable WHERE .....

Everything was fine but When I tried to add SUM function in FastReprot an logical error happend! 一切都很好,但是当我尝试在FastReprot中添加SUM函数时,发生了逻辑错误!

FORMAT function returns the result as a string and SUM function concatenates strings!!! FORMAT函数以字符串形式返回结果,SUM函数将字符串连接起来!

SUM(frxDBDataset1."blabla",MasterData1)

Product1| 123,500,000
Product2| 1,455,999,100
________________________________
SUM = 123,500,000-1,455,999,100

What is best way for showing thousand separator in DBGrid! 在DBGrid中显示千位分隔符的最佳方法是什么? I'm thinking about applying thousand separator in "OnGetText" Event. 我正在考虑在“ OnGetText”事件中应用千位分隔符。 Does this method cause delay in showing data in DBGrid when recordcount>5000 ? 当recordcount> 5000时,此方法是否会导致在DBGrid中显示数据的延迟?

Do you have any suggestions? 你有什么建议吗?

It's really a bad idea to format your results inside the query. 在查询中格式化结果的格式确实是一个坏主意。 The query should be used to retrieve information... presentation should be handled elsewhere. 该查询应用于检索信息...表示应在其他地方处理。 In this case, I would use (I do use, in fact) the DisplayFormat, as suggested in comments. 在这种情况下,我将按照注释中的建议使用(实际上,我确实使用了)DisplayFormat。

You can also write some code to format automatically, if you don't always want to add the fields to the TDataSet on design-time: 如果您不总是希望在设计时将字段添加到TDataSet中,则还可以编写一些代码来自动格式化:

DataSet.Open;
For I := 0 To DataSet.Fields.Count - 1 Do Begin
    If DataSet.Fields[I] Is TFloatField Then
        (DataSet.Fields[I] As TNumericField).DisplayFormat := '###,###,##0.00'
    Else If DataSet.Fields[I] Is TIntegerField Then
        (DataSet.Fields[I] As TNumericField).DisplayFormat := '###,###,##0';
End;

This should be adapted to your actual needs... you could read metadata from BD (this is BD-dependent) to decide on precision, for example. 这应该适合您的实际需求...例如,您可以从BD(取决于BD)读取元数据来决定精度。

Inside FastReport, you could also use DisplayFormat, or you can also use the format function inside your memo: [Format('%.2n', [<DataSet."Field">])] , where the .2 indicates the precision to be used. 在FastReport中,您也可以使用DisplayFormat,或者还可以在备忘录中使用format函数: [Format('%.2n', [<DataSet."Field">])] ,其中.2表示使用。

EDIT: To address the problem of the minus sign appearing on the right with BiDiMode = bdRightToLeft (See this ), I believe you'll really need to use OnGetText event. 编辑:为了解决出现在与右侧的减号的问题BiDiMode = bdRightToLeft (见 ),我相信你会真正需要使用OnGetText事件。 I think you already found this workaround yourself, but I'm including it nevertheless for completeness: 我认为您已经自己找到了解决方法,但是为了完整起见,我还是将其包括在内:

procedure TForm1.FieldGetText(Sender: TField; var Text: String; DisplayText: Boolean);
var
  FmtStr: string;
  F: Double;
begin
  F := Sender.AsFloat;
  FmtStr := TNumericField(Sender).DisplayFormat;
  if Sign(F) = -1 then
    Text := '-' + FormatFloat(FmtStr, Abs(F))
  else
    Text := FormatFloat(FmtStr, F)
end;

With a little change it worked very well. 稍作更改,效果很好。 [ Text := FormatFloat(FmtStr, -F) + '-'; [ 文本:= FormatFloat(FmtStr,-F)+'-'; ] ]

Special thanks to GabrielF. 特别感谢GabrielF。

procedure TForm1.FieldGetText(Sender: TField; var Text: String; DisplayText: Boolean);
var
  FmtStr: string;
  F: Double;
begin
  F := Sender.AsFloat;
  FmtStr := TNumericField(Sender).DisplayFormat;
  if F < 0 then
    Text := FormatFloat(FmtStr, -F) + '-';
  else
    Text := FormatFloat(FmtStr, F)
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM