DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: laidabin
今日帖子: 0
在线用户: 4
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/6 13:31:37
标题:
请教FMX模式下TGrid数据格式化问题 浏览:1873
加入我的收藏
楼主: 大家好,有个问题向大家求教,请大家指导一下,在此谢过了

在FireMonkey模式下,我使用Binding将数据绑定到了TGrid中,我想问问如何在TGrid表中将数据格式化呢

比如将数字:1234567格式化为:1,234,567
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 pcplayer (pcplayer) ★☆☆☆☆ -
普通会员
2023/6/6 14:13:55
1楼: 如果数据来自数据库的 TField,可以在字段的 OnGetValue 事件里面对数字进行格式化。这个方法和 Binding 无关。

如果要在 Binding 里面搞,也可以。在 Binding 的 OnAssigningValue 事件里面写代码。请看:

https://blog.csdn.net/pcplayer/article/details/104290065
----------------------------------------------
-
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/9 20:39:17
2楼: 能具体说说吗,我在OnGetValue方法中试了多种方式,都没有看到生效,不知道是哪里弄好,能否帮忙贴个案例代码

谢谢
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2023/6/9 21:55:35
3楼:
tgrid默认是按数据类型进行基本格式处理的,比如字符串左对齐,数字右对齐,货币类型(currency)自动添加分隔符","。绑定的时候数据类型是取决于数据适配器。

自己处理显示格式时,用TTextLayout来处理内容,不要直接用TCanvas。
----------------------------------------------
-
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/9 23:08:35
4楼: 能具体说明一下吗,FMX还是第一次玩,没什么经验,您说得方法我没啥概念啊
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/10 2:23:14
5楼: in fact, you only needs check "ShowThousandSeparator" on "Columns" in your TGrid using LiveBinding...
此帖子包含附件:
PNG 图像
大小:87.2K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/10 9:35:45
6楼: To:emailx45

非常感谢您的指点,目前的问题是:我的Grid不一定是固定的格式,是根据查询的字段来显示对应的字段,所以针对字段类型与格式需要使用代码来实现,而这正是我不知道如何解决的问题。

您是否能使用动态设置的方式帮忙解决这个问题,谢谢
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2023/6/10 10:22:33
7楼: 动态创建Column?
----------------------------------------------
z@S7
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/10 10:37:18
8楼: 我是将TGRID绑定到了TClientDataSet上,Grid没有设置固定的字段,是根据TClientDataSet的查询结果,显示数据,所以字段名称与数量是不固定的。

现在我的问题是想能够动态的对字段格式进行设置,比如动态的将Grid.Columns[1]的数据设置成千分位格式,或者是百分比格式。

我现在已经做的就是根据查询后的结果设置各字段的宽度,如下:

    Grid1.Columns[0].Visible:=False;
    Grid1.Columns[1].Width:=160;
    Grid1.Columns[2].Width:=100;
    Grid1.Columns[3].Width:=80;
    Grid1.Columns[4].Width:=200;
    Grid1.Columns[5].Width:=80;

但不知如何设置数据的格式。
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2023/6/10 12:13:44
9楼: 你得重新考虑怎么构建整个TGrid了.

你先读取了TClientDataSet,然后跟据TClientDataSet.Fields,依据其每个Field的类型,重新构建TGrid的对应类型的Column,这时候才能依据TColumn的类型设置显示效果。
Columns有了,这个时候再重新做运行时TGrid与TClientDataSet的关系绑定的代码实现。

如果是用TCurrencyColumn(Columns[3]).Format:=''这种,怕是行不通.

总之挺麻烦的,动态创建Columns,动态绑定TGird与TClientDataSet.
此帖子包含附件:
PNG 图像
大小:13.8K
----------------------------------------------
z@S7
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/10 12:27:50
10楼: 或者在FMX模式下有其它控件可以简化这一过程,大家有推荐吗
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2023/6/10 15:52:35
11楼: 或许你可以另辟蹊径,在数据集做文章,例如TFDQuery.FormatOptions

组件的话,可以试试TMS FNX UI Pack 的 TMSFNCGrid + TMSFNCGridDatabaseAdapter
----------------------------------------------
z@S7
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/10 19:55:00
12楼: 目前我临时采用的方式是在SQL查询时就直接就数据格式化,但这会影响到查询的速度,还是想看能否在Grid上面做文章,或者换一个其它的FMX网格控件解决此问题
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/11 0:40:24
13楼: @changfenglee

NO NEED RECREATE THE COLUMNS,   you can try this:

REMEMBER: in Grids (TGrid/StringGrids) all informations is a "string value" -- not the value really showed on the screen!!! - the convertion is done in behind the scenes!!!


implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  for var i: integer := 0 to (Grid1.ColumnCount - 1) do
    begin

// verify if the columns is a "currency, float, single, integer, etc...", for example.... and other informations
//
//
      if (Grid1.Columns[i] is TCurrencyColumn) or { }
        (Grid1.Columns[i] is TFloatColumn) or  { }
        (Grid1.Columns[i] is TIntegerColumn) then
        begin
          TCurrencyColumn(Grid1.Columns[i]).DecimalDigits      := 5;
          TCurrencyColumn(Grid1.Columns[i]).ShowThousandSeparator := true;
          // etc...
        end;
    end;
end;
此帖子包含附件:
PNG 图像
大小:7.0K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/11 0:57:26
14楼: Other way, would be create a new customized "method" to converter or whatever to LiveBinding usage:

here you can learn more about my post in WeDelphi:


How to creating a LiveBindings custom Method to TDate fields to show Empty value on screen! by Emailx45

for example, if your data = 31/12/1899 ( TDate(0) ), in your screen it will be showed as ("") = empty string
http://wedelphi.com/t/422547/
此帖子包含附件:
PNG 图像
大小:20.7K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/11 1:23:22
15楼: my custom function in usage

..
此帖子包含附件:
GIF 图像
大小:126.1K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/11 17:10:58
16楼: TO:emailx45

非常感谢,按您的方法,确实解决了大部分的问题了。

另外还有一个问题想继续问问,就是百分比如何实现呢,比如50.00%这样的
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/12 2:14:17
17楼: the ease way is using "Custom Format" on Columns!

NOTE: but you'll have problem with edit value in non-text types, like: currency, integer, floats, etc...

to advanced usage, it's necessary create yourself "Methods" do livebinding!!!!

NOTE 2: you can use "READ-ONLY" on column or Grid to avoid edit!!!
此帖子包含附件:
PNG 图像
大小:33.9K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2023/6/12 8:33:32
18楼: TO:emailx45

非常感谢,按您的方法,问题已解决,向高手致敬
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/13 1:25:40
19楼: YAAAAHOOOOOOOOOO!
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 sczhyq (旺财) ★☆☆☆☆ -
普通会员
2023/6/16 21:09:11
20楼: 复杂的使用 TGrid.OnDrawColumnCell 事件, 想咋整就咋整


procedure TGridForm.DataGridDrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  LField : String;
  LLayout: TTextLayout;
  LColor : TAlphaColor;
begin

  LField := StockLinkGrid.Columns[Column.Index].MemberName;
  LLayout := TTextLayoutManager.DefaultTextLayout.Create(Canvas);

  try
    try
      LLayout.BeginUpdate;
      LLayout.TopLeft := R.TopLeft;
      LLayout.MaxSize := PointF(Column.Width - 4, DataGrid.RowHeight - 1);
      LLayout.WordWrap := False;
      LLayout.Opacity := Column.AbsoluteOpacity;
      LLayout.HorizontalAlign := TTextAlign.Center;
      LLayout.VerticalAlign := TTextAlign.Center;
      LLayout.Font.Assign(DataGrid.TextSettings.Font);
      LLayout.Trimming := TTextTrimming.Character;
      LColor := DataGrid.TextSettings.FontColor;

      if SameText(LField, 'Flag') then
      case StrToIntDef(LLayout.Text, 0) of
        1 :
        begin
          LLayout.Text := '◇';
          LColor := TAlphaColorRec.Gray;
        end;
        2 :
        begin
          LLayout.Text := '★';
          LColor := TAlphaColorRec.Maroon;
        end;
        3 :
        begin
          LLayout.Text := '☆';
          LColor := TAlphaColorRec.Goldenrod;
        end;
        4 :
        begin
          LLayout.Text := '√';
          LColor := TAlphaColorRec.Green;
        end;
      else
          LLayout.Text := '';
      end;

      LLayout.Color := LColor;

      Canvas.Font.Size := LLayout.Font.Size;
    finally
      LLayout.EndUpdate;
    end;
    LLayout.RenderLayout(Canvas);
  finally
    FreeAndNil(LLayout);
  end;
end;
----------------------------------------------
我84砖家
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2023/6/17 9:12:16
21楼: 一个不知道什么语言的网址,说的挺详细

有些方法在11.0可能是失效了。

Grilles et Livebindings
----------------------------------------------
z@S7
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行159.668毫秒 RSS