cxGridView.DataController.GetRowValue(cxGridView.DataController.GetRowInfo(x), y);
【獲取cxGrid的當前Cell的X,Y座標, 邦定字段, 行列 】
//tv1: TcxGridDBBandedTableView;
uses
cxGridDBTableView
var
ACellViewInfo: TcxGridTableDataCellViewInfo;
AItem: TcxCustomGridTableItem;
iLeft, iTop: Integer;
begin
AItem := tv1.Controller.FocusedItem;
if not SameText(TcxGridDBColumn(AItem).DataBinding.FieldName, 'Price') then
Exit;
//
ACellViewInfo := tv1.Controller.FocusedItem.FocusedCellViewInfo;
//當前編輯Cell的X,Y位置
iLeft := cxGrid1.Left + ACellViewInfo.EditBounds.Left;
iTop := cxGrid1.Top + ACellViewInfo.EditBounds.Top;
iTop := iTop + (ACellViewInfo.EditBounds.Bottom - ACellViewInfo.EditBounds.Top);
//
pnlMyHint.Left := iLeft;
pnlMyHint.Top := iTop + 2;
//
pnlMyHint.Visible := True;
//當前Cell的行與列
edtx.Text := IntToStr(tv1.Controller.FocusedRowIndex);
edtY.Text := IntToStr(tv1.Controller.FocusedColumnIndex);
end;
【取消cxGrid中的交叉色】
将xxxTableView.Styles.StyleSheet清除即可.
【按条件设置行的字体、颜色 - 1】
procedure TfrmPIDaily.cxGrid1DBBandedTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
cxcolumn: TcxGridDBColumn;
iValue: integer;
begin
//判断列是否存在
cxcolumn := (sender as TcxGridDBTableView).GetColumnByFieldName('GroupFooter');
if cxcolumn=nil then exit;
//注意:不能直接取邦定DataSet的值,取出来值不对。
//iValue := MasterDataSet.FieldByName('GroupFooter').AsInteger;
//加一个不显示的列-colGroupFooter(即'GroupFooter')来解决取值问题
iValue:=StrToInt(AViewInfo.GridRecord.Values[colGroupFooter.index]);
if (iValue = 1) then
begin
AViewInfo.Focused;
//加粗、变底色 、白字
ACanvas.Brush.Color := clActiveCaption;
ACanvas.Font.Color := clWhite;
ACanvas.Font.Style := [fsBold];
end;
end;
【按条件设置行的字体、颜色 - 2】
Private
AGreyStyle, AYellowStyle: TcxStyle;
procedure TfrmMIOMonthlyPlus.FormCreate(Sender: TObject);
begin
inherited;
//行颜色
AGreyStyle := TcxStyle.Create(Self);
AGreyStyle.Color := $00EAF3F4;
//
AYellowStyle := TcxStyle.Create(Self);
AYellowStyle.Color := $00EEFFFF;
end;
procedure TfrmMIOMonthlyPlus.cxGrid1DBBandedTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
inherited;
if ARecord.Values[cxGrid1DBBandedTableView1Flag.Index] = 0 then
AStyle := AGreyStyle
else if ARecord.Values[cxGrid1DBBandedTableView1Flag.Index] = 3 then
AStyle := AYellowStyle;
end;
【根据条件设置列的显示颜色】
例:如果单价 < 200000 变红色
procedure TfrmLostBill.colPriceCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
ARec: TRect;
begin
ARec := AViewInfo.Bounds;
if AViewInfo.GridRecord.Values[colPrice.Index] < 200000 then
ACanvas.Canvas.brush.color:=clred;
ACanvas.Canvas.FillRect(ARec);
end;
【根据条件将列设为只读】
例:当QuotBillID不为空时aCode和Price列为只读
uses
cxGridDBTableView
procedure TfrmLostBill.cxGrid1DBBandedTableView1Editing(
Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem;
var AAllow: Boolean);
var
sFieldName: String;
dsDetail: TClientDataSet;
begin
//当前编辑的DataSet
dsDetail := TSmartDataSet(sDetail.DataSet);
//取得当前编辑的字段名
sFieldName := TcxGridDBColumn(AItem).DataBinding.FieldName;
//
if SameText(sFieldName, 'aCode') or SameText(sFieldName, 'Price') then
begin
if not dsDetail.FieldByName('QuotBillID').IsNull then
AAllow := False
else
AAllow := True;
end;
end;
【设定可编辑列的显示Style】
Private
FEditFieldList: String;
procedure TfrmLetMonthly.FormCreate(Sender: TObject);
begin
//指定哪些字段可以编辑
FEditFieldList := UpperCase('[Price][DPrice][SDate][EDate][RDay][Amount][Remark][aChoice]');
inherited;
//设为可编辑
cxGrid1DBBandedTableView1.OptionsData.Editing := True;
end;
procedure TfrmLetMonthly.FormShow(Sender: TObject);
var
sField: String;
i: Integer;
begin
inherited;
//设编辑字段的Style
for i := 0 to cxGrid1DBBandedTableView1.ColumnCount -1 do
begin
sField := cxGrid1DBBandedTableView1.Columns[i].DataBinding.FieldName;
if pos('[' + UpperCase(sField) + ']', FEditFieldList) > 0 then
cxGrid1DBBandedTableView1.Columns[i].Styles.Content := frmCustomResource.cxStyle49;
end;
end;
procedure TfrmLetMonthly.cxGrid1DBBandedTableView1Editing(
Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem;
var AAllow: Boolean);
var
sField: String;
begin
//将不需修改的字段设为只读
if pos('[' + UpperCase(sField) + ']', FEditFieldList) = 0 then
begin
AAllow := False;
Exit;
end;
end;