delphi TcxGrid学习笔记

2022-08-29

更加细致的内容:

https://www.cnblogs.com/usegear/archive/2013/03/24/2978370.html

cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号
cxGrid1DBTableView1.Controller.FocusedRow 当前行 
cxGrid1DBTableView1.Controller.FocusedColumn 当前列 
cxGrid1DBTableView1.Controller.FocusedColumnIndex 当前列号 
cxGrid1DBTableView1.Controller.EditingItem 当前编辑中的单元框
cxGrid1DBTableView1.Controller.EditingController.Edit 当前的编辑框
在编辑状态下可以这样取当前单元格的值: 
if cxGrid1DBTableView1.Controller.FocusedColumn.Editing then 
  ShowMessage(cxGrid1DBTableView1.Controller.EditingController.Edit.EditingValue)
else
  cxGrid1DBTableView1.DataController.GetValue(cxGrid1DBTableView1.DataController.FocusedRecordIndex, cxGrid1DBTableView1.Controller.FocusedItemIndex);
非编辑状态下可以这样取得单元格内的值: OnCellClick事件代码: 
procedure
TForm1.cxGrid1DBTableView1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean); var v : Variant; begin v := ACellViewInfo.Value; end;
取列值
 i := cxGrid1DBBandedTableView1.Controller.FocusedColumn.Index; 
cxGrid1DBBandedTableView1.DataController.GetValue(cxGrid1DBBandedTableView1.Controller.SelectedRows[0].RecordIndex,i);
cxGrid1DBTableView1.DataController.Values[行,列] 
取得焦点
cxGrid1DBTableView1.Columns[5].FocusWithSelection;
cxGrid1DBTableView1.Columns[4].Focused:=True;
得到当前点击的单元格的值 uses   Clipbrd;

OnCellClick事件代码:
procedure   TForm1.cxGrid1DBTableView1CellClick(Sender: TcxCustomGridTableView;  ACellViewInfo: TcxGridTableDataCellViewInfo;  AButton:  TMouseButton; AShift: TShiftState;   var   AHandled:   Boolean); 
var     v   :   Variant; 
begin    
 v := ACellViewInfo.Value;//
 Clipboard.AsText     :=     vartostr(v);//保存到clipboard 
end; cxGrid的DBTableView的名称为dgtv1
1. 返回选中的行数 gdtv1.DataController.GetSelectedCount;
2. 返回选中行的索引: gdtv1.DataController.GetSelectedRowIndex(0) , 表示第一个选中行的索引
3. 返回选中行的数据;
var    I, J:Integer; 
begin
  for I:=0 to gdtv1.DataController.GetSelectedCount - 1 do
  begin
    J := gdtv1.DataController.GetSelectedRowIndex(I);
    ShowMessage(VarToStr(gdtv1.DataController.GetValue(J, 0))); //选择中行的第列的值
  end;
end;
4. 获取cxGrid排序规则 const OrderArray: array[soNone..soDescending] of string = ('None', 'ASC', 'DESC');
var I: integer;
S, OrderStr: string;
begin
  for I := 0 to gdtv1.SortedItemCount - 1 do
  begin
    if S <> '' then
      S := S + ', ';
      OrderStr := OrderStr + gdtv1.SortedItems[I].DataBinding.DefaultCaption + ' ';
      OrderStr := OrderStr + OrderArray[TcxDataSortOrder(gdtv1.SortedItems[I].SortOrder)];
      S := S + OrderStr;
  end;
  ShowMessage('ORDER BY ' + S); end; 5.获取多选的值
for i := 0 to cxgrid1.SelectedRows.Count-1 do
begin
  cxgrid1.DataSource.DataSet.GotoBookmark(Pointer(cxgrid1.SelectedRows.Items[i])); //定位选中的字段 end; 6.//选择行的第1列的值 for I:=0 to cxGrid1DBTableView1.DataController.GetSelectedCount - 1 do
begin
  J := cxGrid1DBTableView1.DataController.GetSelectedRowIndex(I);
  ShowMessage(VarToStr(cxGrid1DBTableView1.DataController.GetValue(J, 0))); end; 7.获取所选行的值 with cxGrid1DBTableView1.Controller do
begin for i:=0 to SelectedRowCount-1 do
   begin
SelectedRows[i].Focused:=True;
    ShowMessage(ADOQuery1.fieldbyname('mc').AsString);
   end; end;

 

 

2022-08-15:

这里强烈建议使用TcxRTTIInspector控件来监视cxGrid1DBTableView1 ,可以在运行期间调试与测试TcxGrid的各种属性的作用,不用频繁的按F9,而且效果是实时展现出来的.

 

一.关联数据库

 

二.显示搜索栏

 

三.OptionsView外观设置.

补充未在图中标示出来的属性:

1.GridLineColor与GridLines:设置网格线颜色是网络线的样式(无/仅竖线/仅横线/全部)

2.ShowColumnFilterButtons: 设置表头上的数据过虑按键何时显示.(默认不显示,总是显示,过虑后显示)

3.ShowEditButtons:在表格中显示编辑按钮(不显示/仅选中行显示/全部显示)

123点综合效果如下:

 

4.RowSeparatorColor与RowSeparatorWidth就是在行与行之间插入间隙,一个设置颜色,一个设置高度(但属性为什么是写width?!),算了不说了,直接看图吧,感觉没什么用

 

 5.NoDataToDisplayInfoText:当没有数据返回时,显示的信息,默认为<No data to display>.你可以自由设置.

 

 

四.OptionsSelection对当前被选中行的设置

如果你希望不带编辑的整行选择,就把cellSelect的勾去掉

 

 

五.OptionsDate,这个没什么好说的,都是字面意思,按自己的业务需求来设置就好.

六.OptionsCustomize.

注意,这里是全局的设置,如果你要单独设置某些列,需要单独选中它们,再从Options里面单独设置

DataRowSizing:是否允许用户调整行高

 

 七.OptionBehavior 行为设置.

   重点关注EditModla的三种模式即可

1.emInplace:不弹窗,点击单元格时,可直接在表内编辑.这是默认的属性

 

 2.emInplaceEditForm:双击单元格时,会在当前记录下方弹出编辑窗体

 

 

3.emInplaceEditFormHideCurrentRow: 双击单元格时,不会显示当前记录,直接弹出编辑窗体

 

 

 

 

 八.导航器设置.(和TDBNavigator功能一致)

Buttons里面可以设置各种按钮的显示

InfoPanel设置上一条/下一条/第一条/最后一条,以及当选记录的位置,里面的visble可以设置它的显示

Visble控制整个导航器的显示

 

 九.FindPanle搜索面板

注意:搜索结果为本地缓存数据搜索,非实时数据搜索.可通过导航面板的刷新按钮来手动刷新结果

 

十.修改字段内容的对齐方式

  设置字段的Properties的值后,在其属性下的Alignment属性下设置

十一.把明文密码设置为*号 

 

2024-04-16 更新:

更多关于选择行的操作,请访问:How to Retrieve the Record Values for the Selected Grid Rows | DevExpress Support

cxGrid1DBTableView1.DataController.DataModeController.GridMode := true;     //设置这个属性时,点击标题栏无法对记录排序
  cxGrid1DBTableView1.DataController.ClearSelection();    //清除当前选择
  cxGrid1DBTableView1.DataController.SelectRows(2, 7);  //自动选择3到8行(第一行是0)

 通过点击按钮,选中上一行或者下一行

procedure TForm1.上一行Click(Sender: TObject);
begin
  cxGrid.SetFocus;
  if aView.Controller.FocusedRowIndex > 0 then
    aView.Controller.FocusedRowIndex := aView.Controller.FocusedRowIndex - 1
  else
    aView.Controller.FocusedRowIndex := aView.Controller.RecordCount - 1;
end;

procedure TForm1.下一行Click(Sender: TObject);
begin
  cxGrid.SetFocus;
  if aView.Controller.FocusedRowIndex < aView.Controller.RecordCount - 1 then
    aView.Controller.FocusedRowIndex := aView.Controller.FocusedRowIndex + 1
  else
    aView.Controller.FocusedRowIndex := 0;
end;

 

posted @ 2022-08-25 14:22  一曲轻扬  阅读(1584)  评论(0)    收藏  举报