本文介绍了Devexpress网格与未绑定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DevExpress网格,我想添加一个未绑定的复选框,以便能够选择一些项目。
选择完成后,我按一个按钮,我必须循环网格以获取所有选定的项目。
它必须是一个复选框。我已经尝试了一个多选择的网格,但用户不能使用它。



我已经尝试了所有的样本,我已经能够找到支持,但是没有运气。




  • 我需要一个未绑定的方法,因为它是一个多用户设置,用户已经相互选择和取消选择。



我的问题:有没有人有一个工作示例来显示如何完成?

解决方案

我已经做到了,这是(是!)很丑陋!创建具有绑定列的网格视图,并添加一个未绑定的复选框列,其字段类型为布尔值。



基本上我处理网格视图的OnCellClick。我检查点击的项目是复选框列 - 通过在复选框类型的视图中找到第一个未绑定的列。然后我切换它的状态。



我已经将数据集上的AutoEdit设置为true,但是删除/编辑/插入为false,ImmediateEditor为false。不确定哪些是重要的。



我认为最难的事情是想解决网格和视图级别对象的复杂层次结构,并制定哪些级别包含哪些的所需位。我相信有一个更好的方法,但现在我们现在有效,我不会再碰它了!



这是从我的代码但是稍微修改并且没有按原样进行测试 - 它还需要更多的错误检查:

 过程TMyForm.ViewCellClick(发件人:TcxCustomGridTableView; 
ACellViewInfo:TcxGridTableDataCellViewInfo; AButton:TMouseButton;
AShift:TShiftState; var AHandled:Boolean);
var
col:TcxGridColumn;
begin
//手动处理复选框单元格的单击 - 否则似乎
//几乎不可能使检查的计数正确。
col:= GetViewCheckColumn(Sender);
if(Sender.Controller.FocusedItem = col)then
begin
ToggleRowSelection(TcxCustomGridTableView(TcxGridSite(Sender).GridView),col);
结束
结束

程序TMyForm.ToggleRowSelection(AView:TcxCustomGridTableView; ACol:TcxGridColumn);
var
rec:TcxCustomGridRecord;
begin
rec:= AView.Controller.FocusedRecord;
if(rec = nil)then exit;

if(rec.Values [ACol.Index] = TcxCheckBoxProperties(ACol.Properties).ValueChecked)then
begin
rec.Values [ACol.Index]:= TcxCheckBoxProperties ACol.Properties).ValueUnchecked;
end
else
begin
rec.Values [ACol.Index]:= TcxCheckBoxProperties(ACol.Properties).ValueChecked;
结束
结束


function TMyForm.GetViewCheckColumn(AView:TcxCustomGridView):TcxGridColumn;
var
index:integer;
vw:TcxCustomGridTableView;
项:TcxCustomGridTableItem;
begin
//我们正在寻找一个未绑定的复选框列 - 我们将返回一个找到的第一个
//。

Assert(AView<> nil);
result:= nil;
if(AView是TcxCustomGridTableView)然后
begin
vw:= TcxCustomGridTableView(AView);
for index:= 0 to vw.ItemCount - 1 do
begin
item:= vw.Items [index];
if(item.Properties是TcxCustomCheckBoxProperties)然后
begin
if(item is TcxGridDBColumn)then
begin
if(TcxGridDBColumn(item).DataBinding.FieldName =' ')然后
begin
result:= TcxGridColumn(item);
break;
结束
结束
结束
结束
结束
结束

然后我通过在网格的OnKeyUp中检查一个空格键来扩展它,并调用ToggleRowSelection和对于一行双击,也是类似的。



当迭代行时,您可以使用以下内容检查行是否被检查:

 函数TMyForm.GetViewIsRowChecked(AView:TcxCustomGridView; ARecord:TcxCustomGridRecord):boolean; 
var
col:TcxGridColumn;
begin
result:= False;
col:= GetViewCheckColumn(AView);
if((col<> nil)和(ARecord<> nil))然后
begin
result:=(ARecord.Values [col.Index] = TcxCheckBoxProperties的.properties).ValueChecked);
结束
结束

我想是这样。我从一个大型网格/视图帮助单元中挖出了一段时间。呵呵,目前正在使用德尔福2010与DXVCL v2011第1.10卷。



希望它有帮助。


I have a DevExpress grid where I would like to add an unbound checkbox to be able to select some of the items.After the selection is made I press a button and I must loop the grid to get all the selected items.It has to be a checkbox. I have tried with a multiselectable grid, but the users can't work with that.

I have tried all the samples that I have been able to find on the supportsites, but no luck.

  • I need the unbound approach since it is a multiuser setup and users have been selecting and deselecting for each other.

My question: does anyone have a working sample that shows how this can be done?

解决方案

I've done this and it was (is!) pretty ugly! Create the grid view with bound columns and add an unbound checkbox column with a field type of boolean.

Basically I handle the OnCellClick of the grid view. I check if the item clicked is the checkbox column - by finding the first unbound column in the view with a checkbox type. Then I toggle its state.

I've set AutoEdit on the dataset to true but Deleting/Editing/Inserting to false and ImmediateEditor is false. Not exactly sure which of those are important.

I think the hardest thing was trying to fathom out the complex hierarchy of grid and view level objects and working out which levels contained which of the needed bits. I'm sure there's a better way of doing it but what we've got now works and I'm not going to touch it again!

This is lifted from my code but modified slightly and not tested as it stands - it also needs a bit more error checking:

procedure TMyForm.ViewCellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  col: TcxGridColumn;
begin
  // Manually handle the clicking of the checkbox cell - otherwise it seems
  // virtually impossible to get the checked count correct.
  col := GetViewCheckColumn(Sender);
  if (Sender.Controller.FocusedItem = col) then
  begin
    ToggleRowSelection(TcxCustomGridTableView(TcxGridSite(Sender).GridView), col);
  end;
end;

procedure TMyForm.ToggleRowSelection(AView: TcxCustomGridTableView; ACol: TcxGridColumn);
var
  rec: TcxCustomGridRecord;
begin
  rec := AView.Controller.FocusedRecord;
  if (rec = nil) then exit;

  if (rec.Values[ACol.Index] = TcxCheckBoxProperties(ACol.Properties).ValueChecked) then
  begin
    rec.Values[ACol.Index] := TcxCheckBoxProperties(ACol.Properties).ValueUnchecked;
  end
  else
  begin
    rec.Values[ACol.Index] := TcxCheckBoxProperties(ACol.Properties).ValueChecked;
  end;
end;


function TMyForm.GetViewCheckColumn(AView: TcxCustomGridView): TcxGridColumn;
var
  index: integer;
  vw: TcxCustomGridTableView;
  item: TcxCustomGridTableItem;
begin
  // We're looking for an unbound check box column - we'll return the first
  // one found.

  Assert(AView <> nil);
  result := nil;
  if (AView is TcxCustomGridTableView) then
  begin
    vw := TcxCustomGridTableView(AView);
    for index := 0 to vw.ItemCount - 1 do
    begin
      item := vw.Items[index];
      if (item.Properties is TcxCustomCheckBoxProperties) then
      begin
        if (item is TcxGridDBColumn) then
        begin
          if (TcxGridDBColumn(item).DataBinding.FieldName = '') then
          begin
            result := TcxGridColumn(item);
            break;
          end;
        end;
      end;
    end;
  end;
end;

I then extended it by checking for a SPACE bar press in the OnKeyUp of the grid and calling ToggleRowSelection and also similar for a double click on a row.

When iterating through the rows you can test if a row is checked using something like the following:

function TMyForm.GetViewIsRowChecked(AView: TcxCustomGridView; ARecord: TcxCustomGridRecord): boolean;
var
  col: TcxGridColumn;
begin
  result := False;
  col := GetViewCheckColumn(AView);
  if ((col <> nil) and (ARecord <> nil)) then
  begin
    result := (ARecord.Values[col.Index] = TcxCheckBoxProperties(col.Properties).ValueChecked);
  end;
end;

I think that's it. I've dug it out of a large grid/view helper unit we've built up over a while. Oh, and it's currently working with Delphi 2010 with DXVCL v2011 vol 1.10.

Hope it helps.

这篇关于Devexpress网格与未绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:14