procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var S : string; begin if Column.FieldName = '你的单位数值字段名称' then//你对字段的显示进行处理 With DBGrid1.Canvas do begin S := DBGrid1.DataSource.DataSet.fields[DataCol].DisplayText + '(公斤)'; DBGrid1.Canvas.TextOut( Rect.Left +1 ,Rect.top+1,S); end; end;其它的类型你可以自己处理字符串变量S了.
用TTbable之类的控件的话不知道能不能。
就是能也很麻烦。
用TQuery之类控件来进行数据操作的话就比较容易实现。
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
S : string;
begin
if Column.FieldName = '你的单位数值字段名称' then//你对字段的显示进行处理
With DBGrid1.Canvas do
begin
S := DBGrid1.DataSource.DataSet.fields[DataCol].DisplayText + '(公斤)';
DBGrid1.Canvas.TextOut( Rect.Left +1 ,Rect.top+1,S);
end;
end;其它的类型你可以自己处理字符串变量S了.
formatdate('YYYY年MM月DD日',Date)
....
//用DBGrid1的OnDrawDataCell事件
//把其中的字段procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
var
Len: Integer;
St: String;
begin
if Field.DataType = ftCurrency then//是货币类型
begin
St := FloatToStrF(Field.AsFloat, ffCurrency,10,2);
Len := Self.DBGrid1.Canvas.TextWidth(St);
Self.DBGrid1.Canvas.TextRect(Rect, Rect.Right - 2 - Len, Rect.Top + 2, St);
Exit;
end; if Field.FieldName = ftDate then//是日期类型
begin
St := Field.AsString; St := FormatDateTime('YYYY', StrToDateTime(St)) + '年' +
FormatDateTime('MM', StrToDateTime(St)) + '月' +
FormatDateTime('DD', StrToDateTime(St)) + '日';
Self.DBGrid1.Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, St);
Exit;
end; if Field.FieldName = '单位数值' then//指定字段名
begin
St := Field.AsString + '(公斤)';
Self.DBGrid1.Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, St);
Exit;
end;
Self.DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
或
if Field.FieldName = ftTime then才行.关键要看是什么数据库和设计时的方式.
//其它方法一样。