CxGrid的使用说明
cxgrid分组自动展开
cxgrdbtblvwGrid1DBTableView1.DataController.Groups.FullExpand
cxgrdbtblvwGrid1DBTableView1.ClearItems;
cxgrdbtblvwGrid1DBTableView1.DataController.CreateAllItems;
(1)动态设置显示格式
procedure SetDisplayFormat(ACtrlData: TClientDataSet;
TbView: TcxGridDBTableView);
var
i: integer;
begin
if ACtrlData.RecordCount <= 0 then Exit;
try
TbView.ClearItems;
ACtrlData.First;
for i := 0 to ACtrlData.RecordCount - 1 do
begin
if ACtrlData.FieldByName('SQBF_DisplayInGrid').AsString = '1' then //在表格中显示
with TbView.CreateColumn do
begin
DataBinding.FieldName := ACtrlData.FieldByName('SQBF_FieldName').AsString;
Caption := ACtrlData.FieldByName('SQBF_Caption').AsString; //字段中文标题
Hint := ACtrlData.FieldByName('SQBF_Hint').AsString;
Width := ACtrlData.FieldByName('SQBF_Width').AsInteger;
HeaderAlignmentHorz := taCenter;
end;
ACtrlData.Next;
end;
except
on E: Exception do
SaveLog('设置显示格式时出错:' + E.Message);
end;
end;
(2)显示行号
procedure TFmQueryBase.cxDBViewMasterCustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
FValue: string;
FBounds: TRect;
begin
FBounds := AViewInfo.Bounds;
if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
begin
ACanvas.FillRect(FBounds);
ACanvas.DrawComplexFrame(FBounds, clBlack, clBlack, [bBottom, bLeft, bRight], 1);
FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
InflateRect(FBounds, -3, -2); //Platform specific. May not work on Linux.
ACanvas.Font.Color := clBlack;
ACanvas.Brush.Style := bsClear;
ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
ADone := True;
end;
end;
(3)设置显示格式,我的项目要求先动态添加字段,这时不知道字段类型,所以设置DisplayFormat不方便,我还没有找到好方法。
所以采用打开数据集后再设置:
procedure TFmQueryBase.cdsMasterAfterOpen(DataSet: TDataSet);
var
i: Integer;
begin
for i := 0 to cxDBViewMaster.DataController.DataSet.FieldCount -1 do
begin
if cxDBViewMaster.DataController.DataSet.Fields[i] is TNumericField then
begin
if Pos('AMOUNT', UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then
begin
TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := '#,##0.000';
Continue;
end;
if Pos('QUANTITY', UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then
begin
TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := '#,##0.000';
Continue;
end;
if Pos('MONEY', UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then
begin
TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := '#,##0.00';
Continue;
end;
end;
end;
end;