Have cxGrid expand current date

2019-07-29 07:31发布

问题:

I made my grid group by date (grabbed the column name and dragged it to where it says 'group by that column'). However, when the grid is displayed all the dates are 'closed' so I must expand them to see data. That is OK but I wonder if it is possible to have current date expanded already (all other should remain closed !) so I do not have to click the expand cross?

回答1:

try this, you can put the code in other event handler like a TButton for example

procedure TForm1.FormCreate(Sender: TObject);
begin
    //aDBTableView1.ViewData.Expand(true);            // this is how to expand all records 
    aDBTableView1.ViewData.Records[YourRecordNumber].Expand(true);   // this is how to expand by a given record
end;

OK try the following

procedure TForm1.FormCreate(Sender: TObject);
begin
  with cxGrid1DBTableView1 do
  begin
    DataController.DataSource.DataSet.Locate('YourDateFieldName',DateTimeToStr(Date),
    [loPartialKey]);
    ViewData.Records[DataController.FocusedRowIndex].Expand(True);
  end;
end;


回答2:

procedure TForm1.Button1Click(Sender: TObject);
var
  intLoop,
  vValue: Variant;
begin
  for intLoop := 0 to self.cxGrid1DBTableView1.DataController.RowCount - 1 do
  begin
    if self.cxGrid1DBTableView1.ViewData.Rows[IntLoop] is TcxGridGroupRow then
    begin
      if TcxGridGroupRow(cxGrid1DBTableView1.ViewData.Rows[IntLoop]).Level = cxGrid1DBTableView1MyDate.GroupIndex  then
      begin
        vValue:=TcxGridGroupRow(cxGrid1DBTableView1.ViewData.Rows[IntLoop]).Value ;
        if vValue = Date() then
        begin
          TcxGridGroupRow(cxGrid1DBTableView1.ViewData.Rows[intLoop]).Expand(False);
        end;
      end;
    end;
  end;
end;