搜档网
当前位置:搜档网 › cxgrid使用方法

cxgrid使用方法

cxgrid使用方法
cxgrid使用方法

CXGRID颜色设置

DELPHI技术2010-09-05 20:03:54 阅读83 评论0 字号:大中小订阅

1、隔行显示

procedure TForm1.cxGrid1DBT ableView1CustomDrawCell(

Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;

AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);

begin

if AViewInfo.GridRecord.Values[0] mod 2 = 0 then

ACanvas.Canvas.brush.color := clred;

end;

2、根据条件设置行颜色

procedure TFrmSalesHis.View1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);

begin

if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('isgd').Index] = true then

begin

ACanvas.Canvas.Brush.Color := clred;

ACanvas.canvas.Font.Color := clblack;

end;

end;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////

var

AYellowStyle: TcxStyle;

procedure TForm1.FormCreate(Sender: TObject);

begin

//行颜色

AYellowStyle := TcxStyle.Create(Self);

AYellowStyle.Color := $0080FFFF;

AYellowStyle.TextColor := clMaroon;

end;

procedure TForm1.cxGrid1DBBandedTableView1StylesGetContentStyle(

Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;

AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);

begin

if ARecord.Values[cxGrid1DBBandedTableView1Lengthcm.Index] < 81 then AStyle := AYellowStyle;

end;

这里cxGrid1DBBandedTableView1Lengthcm.Index小于81时就显示黄色

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if vartostr(ARecord.Values[8]) = '已审' then

AStyle := AYellowStyle;

ARecord.Values[8] :cxgrid中第8列的值。。

/////////////////////////////////////////////////////////////////////////////////////////////////////////

3、设置行列颜色

procedure TForm1.cxGrid1DBT ableView1CustomDrawCell(

Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;

AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);

begin

//Item确定列(ID列的索引),RecordViewInfo确定行(Index行的索引)

//可以定位到某一行改变颜色

{if (AViewInfo.Item.ID = 0) and (AViewInfo.RecordViewInfo.Index = 0) then

ACanvas.Brush.Color := clred;}

//可以根据条件值确定改变某行颜色

if (trim(AViewInfo.RecordViewInfo.GridRecord.Values[4]) = 'HTT')

and (AViewInfo.Item.ID = 4) //确定到某一列,如果不加确定是某行底色

then

ACanvas.Brush.Color := clred;

end;

CxGrid控制行颜色

文章分类:综合技术

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(

Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;

AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); begin

//Item确定列(ID列的索引),RecordViewInfo确定行(Index行的索引)

//可以定位到某一行改变颜色

{if (AViewInfo.Item.ID = 0) and (AViewInfo.RecordViewInfo.Index = 0) then

ACanvas.Brush.Color := clred;}

//可以根据条件值确定改变某行颜色

if (trim(AViewInfo.RecordViewInfo.GridRecord.Values[4]) = 'HTT')

and (AViewInfo.Item.ID = 4) //确定到某一列,如果不加确定是某行底色 then

ACanvas.Brush.Color := clred;

end;

CXGrid控件整行变颜色

CXGrid控件整行变颜色当某行符合条件的时候,给它变色:if not VarIsNull(A ViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldNam e('Status').Index]) then if A ViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('Status'). Index] = '选中' then begin ACanvas.Canvas.Font.Color := clBlack; ACanvas.Brush.Color := clRed; end;

2.去掉"Drag a column header here to group by that column" 方法

cxGrid1DBTableView1.OptionsView.GroupByBox置为False

3.去掉表头下三角数据

方法

cxGrid1DBTableView1.Optionscustomize.columnfiltering置为False

4.增加序号

方法

在dataset 里边增加Mycount 字段类型为string

在CXgrid 增加显示字段序号mycount

为该字段写事件

procedure Tfrm_form.ReDataSet2mycountGetText(Sender: TField;

var Text: String; DisplayText: Boolean);

begin

inherited;

text:=inttostr(redataset2.RecNo);

end;

将序号绑定字段Mycount

5.CXgrid 增加一栏显示checkBox

方法

在dataset 里边增加MySelect字段类型为BOOLEAN

在CXgrid 增加显示字段选择select

设定select 字段的Properties为CheckBox . ReadOnly = False;

NullStyle = nssUnchecked

procedure Tfrm_form.cxGrid1DBTableView1CellClick(

Sender: TcxCustomGridTableView;

ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);

var

Row: Integer;

begin

inherited;

if https://www.sodocs.net/doc/ff1254255.html, = 'mycheck' then

begin

Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;

if cxGrid1DBTableView1.ViewData.Records[Row].Values[0] = True then cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := False else

cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := True; end;

end;

procedure Tfrm_form.cxGrid1DBTableView1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

var

Row: Integer;

begin

inherited;

//单选

Delphi Cxgrid 使用方法默认分类2010-04-16 16:06:52 阅读442 评论0 字号:大中小订阅

Delphi cxgrid 使用方法

1.绑定数据

方法

cxGrid1DBTableView1.DataController.DataSource:=DataSource1

2.去掉"Drag a column header here to group by that column"

方法

cxGrid1DBTableView1.OptionsView.GroupByBox置为False

3.去掉表头下三角数据

方法

cxGrid1DBTableView1.Optionscustomize.columnfiltering置为False

4.增加序号

方法

在dataset 里边增加Mycount 字段类型为string

在CXgrid 增加显示字段序号mycount

为该字段写事件

procedure Tfrm_form.ReDataSet2mycountGetText(Sender: TField;

var Text: String; DisplayText: Boolean);

begin

inherited;

text:=inttostr(redataset2.RecNo);

end;

将序号绑定字段Mycount

5.CXgrid 增加一栏显示checkBox

方法

在dataset 里边增加MySelect字段类型为BOOLEAN

在CXgrid 增加显示字段选择select

设定select 字段的Properties为CheckBox . ReadOnly = False; NullStyle = nssUnchecked

procedure Tfrm_form.cxGrid1DBTableView1CellClick(

Sender: TcxCustomGridTableView;

ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);

var

Row: Integer;

begin

inherited;

if https://www.sodocs.net/doc/ff1254255.html, = 'mycheck' then

begin

Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;

if cxGrid1DBTableView1.ViewData.Records[Row].Values[0] = True then cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := False

else

cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := True; end;

end;

procedure Tfrm_form.cxGrid1DBTableView1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

var

Row: Integer;

begin

inherited;

//单选

// for Row:=0 to cxGrid1DBTableView1.DataController.RecordCount-1 do

// begin

// cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := False;

// end;

//多选

if cxGrid1DBTableView1.DataController.RecordCount<>0 then

begin

Row := cxGrid1DBTableView1.DataController.FocusedRecordIndex;

if cxGrid1DBTableView1.ViewData.Records[Row].Values[0] = True then

cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := False

else

cxGrid1DBTableView1.ViewData.Records[Row].Values[0] := True;

end;

end;

从ini文件中读出并设置

cxGrid1DBTableView1.RestoreFromIniFile('***.ini', false);

将设置保存到ini文件

cxGrid1DBTableView1.StoreToIniFile('***.ini', false);

=================

cxGrid里的数据导入到EXCEL,HTML,XML和TEXT(利用CXGRID自带的函数ExportGridToExcel,ExportGridToText,ExportGridToXML,ExportGridToHTML)

interface

uses cxGridExportLink;

begin

ExportGridToEXCEL('d:\hk.xsl',cxGrid1,True,True);

ExportGridToTEXT('d:\hk.txt',cxGrid1,True,True);

ExportGridToXML('d:\hk.xml',cxGrid1,True,True);

ExportGridToHTML('d:\hk.html',cxGrid1,True,True);

{ if DialogSave1.Execute then

begin

ExportGridToEXCEL(DialogSave1.FileName, cxGrid1, True, True);

end;}

end;

在主从TableView中根据主TableView得到对应的从TableView

var

ADetailDC: TcxGridDataController;

AView: TcxCustomGridTableView;

begin

with cxGrid1DBTableView1.DataController do

ADetailDC :=

TcxGridDataController(GetDetailDataController(FocusedRecordIndex, 0)); AView := ADetailDC.GridView;

end;

=====================================================================

=========

定位在第一行并显示内置编辑器

cxDBVerticalGrid1.FocusedRow := cxDBVerticalGrid1.Rows[0]; cxDBVerticalGrid1.ShowEdit;

=====================================================================

=========

隐藏 "" 字符串

该文本存储在scxGridNoDataInfoText资源字符串,可以将该资源字符串的内容设为空

来隐藏该文本。

uses cxClasses, cxGridStrs;

...

cxSetResourceString(@scxGridNoDataInfoText, '');

//如果"" 字符串已经显示,需要调用:

.LayoutChanged;

============================================================

删除应用过滤后的行

var

I: Integer;

begin

with do

for I := 0 to ViewData.RecordCount - 1 do

begin

ViewData.Records[0].Focused := True;

DataController.DataSet.Delete;

end;

=============================================================

根据单元的值设置样式

procedure .StylesGetContentStyle(

Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);

begin

if ARecord.Values[AItem.Index] = aSomeValue then

AStyle := ;

end;

procedure .StylesGetContentStyle(

Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);

AColumn: TcxCustomGridTableItem;

begin

AColumn := (Sender as TcxGridDBTableView).GetColumnByFieldName('Email'); if VarToStr(ARecord.Values[AColumn.Index]) = '' then

AStyle := cxStyleNullEmail;

end;

=====================================================================

=========

TcxCustomGridTableView.FindItemByName,

TcxGridDBTableView.GetColumnByFieldName or

TcxGridDBDataController.GetItemByFieldName

with cxGrid1DBBandedTableView1.DataController do

AValue := Values[FocusedRecordIndex,

GetItemByFieldName('SomeFieldName').Index];

===================================================================

动态生成BandedView

var

AView: TcxCustomGridView;

begin

AView := .CreateView(TcxGridDBBandedTableView); TcxGridDBBandedTableView(AView).DataController.DataSource :=

;

TcxGridDBBandedTableView(AView).Bands.Add;

with TcxGridDBBandedTableView(AView).Bands.Add do

begin

Visible := False;

FixedKind := fkLeft;

end;

TcxGridDBBandedTableView(AView).DataController.CreateAllItems;

.GridView := AView;

=====================================================================

=

当底层数据集为空时显示一条空记录

procedure

.Enter(Sender: TObject);

View: TcxGridDBTableView;

begin

View := TcxGridDBTableView((Sender as TcxGrid).FocusedView);

if View.DataController.DataSet.IsEmpty then

begin

View.DataController.DataSet.Append;

View.Controller.EditingController.ShowEdit;

end;

end;

===================================================================== ==

在当前View插入记录

使用FocusedView属性得到当前焦点View,用View.DataController得到对应的Data Controller,

之后使用Data Controller的方法来操作数据:

- Append

- Insert

- Post

- Cancel

- DeleteFocused

- DeleteSelection

示例:

var

ARecIndex: Integer;

View.DataController.Append;

ARecIndex := View.DataController.FocusedRecordIndex;

View.DataController.Values[ARecIndex, SomeItemIndex] := SomeValue; View.DataController.Post;

另外一种方法是使用View.DataController.DataSource.DataSet得到底层数据集后,再用数据集的

方法来操作数据。

===================================================================== ===

激活内置编辑控件

1) .Controller.EditingController.ShowEdit();

2)

.Controller.EditingController.StartEditShowingTimer();

3) .Controller.EditingItem := ;

4) .Editing := True;

隐藏内置编辑控件

.Controller.EditingController.HideEdit(True);

=====================================================================

======

移除一个分组列

.GroupIndex := -1;

.Visible := True;

=====================================================================

======

保存修改到数据库

procedure .FormClose(Sender: TObject; var Action: TCloseAction); begin

if (.FocusedView <> nil) and

(.FocusedView.DataController.EditState <> []) then

.FocusedView.DataController.Post;

end;

=====================================================================

=======

设置内置右键菜单

内置右键菜单包括二个菜单:cxGridStdHeaderMenu, TcxGridStdFooterMenu uses cxGridStdPopupMenu;

procedure TForm1.cxGridPopupMenu1Popup(ASenderMenu: TComponent; AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean); begin

if ASenderMenu is TcxGridStdHeaderMenu then

TcxGridStdHeaderMenu(ASenderMenu).OnPopup := StdHeaderMenuPopup;

end;

procedure TForm1.StdHeaderMenuPopup(Sender: TObject);

var

I: Integer;

begin

with TcxGridStdHeaderMenu(Sender).Items do

for I := 0 to Count - 1 do

if Items[I].Caption = 'Group By Box' then

begin

Items[I].Enabled := False;

System.Break;

end

end;

===================================================================== ======

得到选中记录的值

1) View.DataController.DataModeController.GridMode = False时

RecIdx := View.Controller.SelectedRecords[i].RecordIndex;

ColIdx := View.DataController.GetItemByFieldName(AFieldName).Index; OutputVal := View.DataController.Values[RecIdx, ColIdx];

//RecID := View.DataController.GetRecordId(RecIdx);

//OutputVal := ADataSet.Lookup(View.DataController.KeyFieldNames, RecID, AFieldName);

2) View.DataController.DataModeController.GridMode = True时

Bkm := View.DataController.GetSelectedBookmark(ASelectedRecordIndex); if ADataSet.BookmarkValid(TBookmark(Bkm)) then

begin

ADataSet.Bookmark := TBookmark(Bkm);

OutputVal := ADataSet.FieldByName(AFieldName).Value;

end;

View.BeginUpdate;

View.DataController.BeginLocate;

try

// make changes here…

finally

View.DataController.EndLocate;

View.EndUpdate;

end;

=============================================================

在GridMode禁用内置的右键Footer菜单

uses cxGridStdPopupMenu;

procedure cxGridPopupMenuOnPopup(...)

begin

if (ASenderMenu is TcxGridStdFooterMenu) and

.DataController.DataModeController.GridMode then AllowPopup := False;

end;

============================================================== 主从表任何时候只能展开一个组

procedure TForm1.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIndex: Integer; var AAllow: Boolean);

var

I: Integer;

C: Integer;

begin

AAllow := False;

C := 0;

for I := 0 to ADataController.RecordCount - 1 do

begin

if ADataController.GetDetailExpanding(I) then

Inc(C);

if C > 1 then

AAllow := True;

end;

end;

procedure TForm1.ADetailDataControllerExpanding( ADataController: TcxCustomDataController; ARecordIndex: Integer; var AAllow: Boolean);

begin

ADataController.CollapseDetails;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

cxGrid1DBTableView1.DataController.OnDetailExpanding := ADetailDataControllerExpanding;

cxGrid1DBTableView1.DataController.OnDetailCollapsing := ADetailDataControllerCollapsing;

end;

=================================================================

动态创建层次(Level)和视图(View)

var

Grid: TcxGrid;

Level: TcxGridLevel;

View: TcxGridDBTableView;

begin

// Creates a Grid instance

Grid := TcxGrid.Create(SomeOwner);

Grid.Parent := SomeParent;

// Creates a Level

Level := Grid.Levels.Add;

https://www.sodocs.net/doc/ff1254255.html, := 'SomeLevelName';

// Creates a View

View := Grid.CreateView(TcxGridDBTableView) as TcxGridDBTableView; https://www.sodocs.net/doc/ff1254255.html, := 'SomeViewName';

// … and binds it to the Level

Level.GridView := View;

// Hooks up the View to the data

View.DataController.DataSource := SomeDataSource;

// … and creates all columns

View.DataController.CreateAllItems;

end;

===================================================================== =

获得Group Footer合计行对应的记录

procedure TForm1.cxGrid1DBTableView1CustomDrawFooterCell(

Sender: TcxGridTableView; ACanvas: TcxCanvas;

AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: Boolean);

var

ALevel, ADataGroupIndex: Integer;

AGridRecord, AGroupRecord: TcxCustomGridRecord;

begin

if AViewInfo is TcxGridRowFooterCellViewInfo and // Row footer (TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName = 'Area')

then // Area column

begin

AGridRecord := TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord; ALevel := TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel; ADataGroupIndex :=

Sender.DataController.Groups.DataGroupIndexByRowIndex[AGridRecord.Ind ex];

if ADataGroupIndex <> -1 then

begin

AGroupRecord := AGridRecord;

while AGroupRecord.Level <> ALevel do

AGroupRecord := AGroupRecord.ParentRecord;

AViewInfo.Text := AGroupRecord.DisplayTexts[0];

end;

end;

end;

=====================================================================

======

访问过滤之后的记录

var

I: Integer;

begin

Memo1.Lines.Clear;

with cxGrid1DBTableView1.DataController do

for I := 0 to FilteredRecordCount - 1 do

Memo1.Lines.Add(DisplayTexts[FilteredRecordIndex[I], 0]);

end;

=====================================================================

=======

获得单元的Font

cxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items[1].GetCellViewInfo ByItem(

cxGrid1DBTableView1Company).EditViewInfo.Font;

===================================================================== =======

根据Level名称找到Level对象

function GetLevelByName(AGrid: TcxGrid; ALevelName: string): TcxGridLevel;

function LoopThroughLevels(ALevel: TcxGridLevel; ALevelName: string): TcxGridLevel;

var

I: Integer;

begin

Result := nil;

for I := 0 to ALevel.Count - 1 do

begin

if ALevel[I].Name = ALevelName then

begin

Result := ALevel[I];

Exit;

end;

if ALevel[I].Count > 0 then

begin

Result := LoopThroughLevels(ALevel[I], ALevelName);

if Result <> nil then

Exit;

end;

end;

end;

var

I: Integer;

begin

Result := nil;

for I := 0 to AGrid.Levels.Count - 1 do

begin

if AGrid.Levels[I].Name = ALevelName then

begin

Result := AGrid.Levels[I];

Exit;

end;

if AGrid.Levels[I].Count > 0 then

begin

Result := LoopThroughLevels(AGrid.Levels[I], ALevelName);

if Result <> nil then

Exit;

end;

end;

end;

=====================================================================

=======

指定Filter Builder打开/保存过滤文件的默认路径

uses

..., cxFilterControlDialog;

procedure TForm.GridView1FilterControlDialogShow(

Sender: TObject);

begin

TfmFilterControlDialog(Sender).OpenDialog.InitialDir := 'D:\'

end;

=====================================================================

=======

保存/恢复带汇总行的布局

.StoreToIniFile('c:\Grid.ini', True, [gsoUseSummary]);

.RestoreFromIniFile(,True,False {or True, optional},[gsoUseSummary]);

=====================================================================

=======

取消过滤时移到第一行

uses

cxCustomData;

procedure TYour_Form.AViewDataControllerFilterChanged(Sender: TObject); var

Filter: TcxDataFilterCriteria;

begin

with Sender as TcxDataFilterCriteria do

if IsEmpty then

DataController.FocusedRowIndex := 0;

end;

===================================================================== ========

排序后移到第一行

可以设置DataController.Options.FocusTopRowAfterSorting := True,也可

以使用如下的代码:

uses

cxCustomData;

procedure TYour_Form.Your_ViewDataControllerSortingChanged(Sender: TObject);

begin

TcxCustomDataController(Sender).FocusedRowIndex := 0;

end;

===================================================================== =========

判断当前行是否第一行或最后一行

可以使用DataController的IsBOF, IsEOF方法,或者:

.Controller.Controller.FocusedRow.IsFirst

.Controller.Controller.FocusedRow.IsLast

===================================================================== =========

根据指定值查找记录

DataController提供了好几个方法来得到指定值对应的RecordIndex

对于Bound View可以使用FindRecordIndexByKeyValue方法

===================================================================== ==========

编辑和显示Blob字段

该字段的Properties设置为BlobEdit,并将BlobPaintStyle 属性设为 bpsText

=====================================================================

相关主题