Search code examples
delphidevexpresstcxgrid

Delphi, expand grouped by grid by default


I have this procedure for a grid in Delphi, and I need to add this property to expand all collapsed grouped by data in the grid.

procedure TProjectForm.dxDBGridEventDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
 DrawHighlight(ACanvas);
 TcxGridDbTableView.ViewData.Expand(True);
end;

I get the following error:

E2233 Property 'ViewData' inaccessible here

Help is appreciated please. And I also need to remove the collapsible button for the grouped data in this grid. Thank you


Solution

  • You can call cxGrid1DBTableView1.ViewData.Expand(True) without problems as long as you don't do in in a drawing event like the one in your q. However, you don't actually need to do this if you use the example below.

    This works fine

    procedure TDevexGroupingForm.FormCreate(Sender: TObject);
    begin
      cxGrid1DBTableView1.Columns[2].GroupIndex := 0;  //  group by the 3rd column
      //  NOTE:  this step is only necessary if the table view has not been grouped at design-time
    
      cxGrid1DBTableView1.DataController.Options := cxGrid1DBTableView1.DataController.Options
       + [dcoGroupsAlwaysExpanded];  // this hides the +/- buttons of the grouped nodes
    
      cxGrid1DBTableView1.DataController.FocusedRowIndex := 0;  // focuses the first group
    end;
    

    Note : This has been updated at @Nil's suggestion.

    • The first line, setting a column's GroupIndex is only necessary if the TableView has not already been grouped at design time.

    • Setting the FocusedRowIndex is optional, depending on how you want the TableView to display initially

    • So in fact the hiding of the +/- grouping buttons and the expansion of all the top-level group nodes can be achieved by the single step of setting the DataController Options property to include the dcoGroupsAlwaysExpanded option.

    Btw Setting the DataController options to suppress the drawing of the expand/collapse buttons and is derived from the article https://www.devexpress.com/Support/Center/Question/Details/Q105527/how-do-i-hide-the-expand-button-on-a-grid