2013-01-17 41 views
2

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

我已经尝试了所有能够在支撑物上找到的样品,但没有运气。

  • 我需要非绑定的方法,因为它是一个多用户设置,用户一直在为对方选择和取消选择。

我的问题:有没有人有一个工作示例,说明如何做到这一点?

+4

你的问题到底是什么?它是如何添加未绑定的列?或者如何“循环网格”?还是其他什么东西?你在这里根本不问任何问题;你刚刚发布了几个声明。此外,您是否看到[DevExpress GridControl Unbound Columns](http://stackoverflow.com/q/6432940/62576)? –

回答

1

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

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

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

我认为最困难的事情是试图了解网格和视图级别对象的复杂层次结构,并确定哪些级别包含哪些所需的位。我确信有更好的方法去做,但我们现在有的工作,我不会再碰它!

这是从我的代码解除,但略作修改,并没有经过测试,因为它代表 - 它也需要多一点的错误检查:

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; 

然后我通过检查空格键按在的onkeyup扩展它的网格并调用ToggleRowSelection,并且双击一行也是类似的。

当通过某一行使用类似下面的检查可以测试行迭代:

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; 

我认为这是它。我已经从一个我们已经建立了一段时间的大型网格/视图帮助单元中挖出它。哦,目前它正在与德尔福2010年与DXVCL v2011第1.10卷。

希望它有帮助。

+0

我其实在丹麦的一位朋友那里得到了一个示例应用程序。 如果您有兴趣,我可以问他是否可以与您分享。 使用起来非常简单快捷。 – OZ8HP

+0

感谢您的提议。但我希望我们现在可以把这个项目放在后面,所以不要去任何麻烦。 – shunty