为了在cxGrid中实现展开当前组时收起其他组的特殊效果,先找遍了cxGridDBTableView的事件,愣是没找到点击分组前面加号“+”的事件。在google里搜索了老半天,终于在搜索“cxGrid group Expand event”关键字时在Dev Express的官网论坛找到了解决方案:可以在View的OnMouseDown里通过判断HitTest是否为TcxGridExpandButtonHitTest来实现,具体代码如下(原文链接:How to make a cxGrid expand only one group at a time,代码稍稍整理了一下):
procedure TForm1.cxGrid1DBTableView1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
AView: TcxGridTableView;
AHitTest: TcxCustomGridHitTest;
I: Integer;
begin
// Note that the Sender parameter is a Site
AView := (Sender as TcxGridSite).GridView as TcxGridTableView;
AHitTest := AView.ViewInfo.GetHitTest(X, Y);
// The point belongs to the [+]/[-] button area ?
if (AHitTest is TcxGridExpandButtonHitTest) and
not TcxGridExpandButtonHitTest(AHitTest).GridRecord.IsData and
TcxGridGroupRow(TcxGridExpandButtonHitTest(AHitTest).GridRecord).Expanded then begin
with AView.ViewData do begin
for I := RowCount - 1 downto 0 do
if (Rows[I] is TcxGridGroupRow) and TcxGridGroupRow(Rows[I]).Expanded and
not (Rows[I] = TcxGridExpandButtonHitTest(AHitTest).GridRecord) then begin
(Rows[I] as TcxGridGroupRow).Collapse(false);
end;
end;
end;
end;
以上曲线救国的方法总算实现了分组加号“+”点击事件的监控。不过有点奇怪的是:以cxGrid如此强大的功能,为什么不直接放一个类似dxDBGrid.OnExpanded的事件呢?那样用起来就比在OnMouseDown里通过HitTest判断方便多了。
附录:
在cxGrid帮助中查询HitTest时发现一个好东西:在devexpress\expressquantumgrid 6\help\expressquantumgrid6.hlp中查找“Grid View Elements”,可以看到cxGrid各组成部分的详细示例图!呵呵,对掌握cxGrid的用法绝对有很大帮助,看一下就知道了。