Delphi DBGrid 固定小數位數的实现(形如1,000,000.00)

在DBGrid的DBGridDrawColumnCell事件中嵌入如下代码:

var
 strShow: string;
begin
  if (Column.FieldName='lastyear') or (Column.FieldName='thisyear') then              //lastyear和thisyear为需要格式化输出的数据表中的字段。
    begin
      if not Column.Field.IsNull then
        strShow := FormatFloat(',0.00', Column.Field.AsFloat) //',0.00'是显示格式。
      else
        strShow := '';
      Column.Field.Alignment := taRightJustify;
      if Rect.Right - Rect.Left < dbgrid1.Canvas.TextWidth(strShow) then
        strShow := Copy(strShow, 1, Trunc(Length(strShow) * (Rect.Right - Rect.Left)
          / (dbgrid1.Canvas.TextWidth(strShow) + 2)));
      dbgrid1.Canvas.FillRect(Rect);
      dbgrid1.Canvas.TextOut(Rect.Right - dbgrid1.Canvas.TextWidth(strShow) - 2,
        Rect.Top + 2, strShow);
      Exit;
    end;
end;

posted @ 2010-05-28 16:14  我心飛揚  阅读(613)  评论(0编辑  收藏  举报