下列代码是仅让一行展开: procedure TForm1.cxGrid1DBTableView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var AHitTest: TcxCustomGridHitTest; I: Integer; begin AHitTest:=cxGrid1DBTableView1.ViewInfo.GetHitTest(X, Y);//取点击的位置 if (AHitTest is TcxGridExpandButtonHitTest) and not TcxGridExpandButtonHitTest(AHitTest).GridRecord.IsData then//点击位置是"+"按钮和不在数据单元 if TcxGridGroupRow(TcxGridExpandButtonHitTest(AHitTest).GridRecord).Expanded then//如果当前动作是展开 with cxGrid1DBTableView1.ViewData do for I := RowCount - 1 downto 0 do //收拢其他被展开的项 if (Rows[I] is TcxGridGroupRow) and (Rows[I] as TcxGridGroupRow).Expanded then if not (Rows[I] = TcxGridExpandButtonHitTest(AHitTest).GridRecord) then (Rows[I] as TcxGridGroupRow).Collapse(false); end;
这个是CXGRID典型的层的思想,主从结构字段设置好了,一个代码都不用写,就可以达到这样的效果。
断电调试的时候在if (AHitTest is TcxGridExpandButtonHitTest) and not TcxGridExpandButtonHitTest(AHitTest).GridRecord.IsData then 直接跳出了 没进去?
procedure TForm1.cxGrid1DBTableView1DataControllerDetailExpanding( ADataController: TcxCustomDataController; ARecordIndex: Integer; var AAllow: Boolean);//这个事件在展开从表时触发 begin cxGrid1DBTableView1.ViewData.Collapse(True);//收拢所有之前展开的从表,使主表记录永远只有一笔展开 cxGrid1DBTableView1.Controller.FocusedRecordIndex:=ARecordIndex;//把焦点移到展开的主表记录上 end;
procedure TForm1.cxGrid1DBTableView1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
AHitTest: TcxCustomGridHitTest;
I: Integer;
begin
AHitTest:=cxGrid1DBTableView1.ViewInfo.GetHitTest(X, Y);//取点击的位置
if (AHitTest is TcxGridExpandButtonHitTest) and not TcxGridExpandButtonHitTest(AHitTest).GridRecord.IsData then//点击位置是"+"按钮和不在数据单元
if TcxGridGroupRow(TcxGridExpandButtonHitTest(AHitTest).GridRecord).Expanded then//如果当前动作是展开
with cxGrid1DBTableView1.ViewData do
for I := RowCount - 1 downto 0 do //收拢其他被展开的项
if (Rows[I] is TcxGridGroupRow) and (Rows[I] as TcxGridGroupRow).Expanded then
if not (Rows[I] = TcxGridExpandButtonHitTest(AHitTest).GridRecord) then
(Rows[I] as TcxGridGroupRow).Collapse(false);
end;
ADataController: TcxCustomDataController; ARecordIndex: Integer;
var AAllow: Boolean);//这个事件在展开从表时触发
begin
cxGrid1DBTableView1.ViewData.Collapse(True);//收拢所有之前展开的从表,使主表记录永远只有一笔展开
cxGrid1DBTableView1.Controller.FocusedRecordIndex:=ARecordIndex;//把焦点移到展开的主表记录上
end;