1

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

nil
  • 1,320
  • 1
  • 10
  • 21
AM AM
  • 55
  • 1
  • 6
  • `Sender.ViewData.Expand(True);`? If that is not accessible `TcxGridDbTableView(Sender).ViewData.Expand(True);`. – Victoria Sep 22 '17 at 13:24
  • @Victoria, thank you, when I try this, i get an access violation and it doesn't expand.. – AM AM Sep 22 '17 at 13:28
  • @Victoria The last one doesn't work either. But it doesn't give me any error, it just doesn't work.. – AM AM Sep 22 '17 at 13:30
  • I think you may be on a hiding to nothing trying do do this in a DrawCell event as it will force a re-draw. Also, what do you mean by "remove the collapsible button"? Do you mean you want to prevent the expand/collapse button for each group from being drawn? – MartynA Sep 22 '17 at 13:34
  • @MartynA I don't really know the best way to do this. I am quite new to Delphi. What I need is to group the data by date, but it should expanded by default and the expand/collapse buttoon should be hidden, it shouldn't be active.. – AM AM Sep 22 '17 at 13:35
  • I would not expand during painting. DevExpress Grids typically first calculate ViewInfo if anything important for that changes, then paint with the previously calculated ViewInfo, and re-use that unless it needs recalculating. What you are trying to do would invalidate the calculated ViewInfo, you would not get what you like. – nil Sep 22 '17 at 13:36
  • Well, you can call ` cxGrid1DBTableView1.ViewData.Expand(True);` w/o problems as long as you don't do in in an event like that. For the button-drawing, you might do best to ask Devex support or at least have a look there - I vaguely recall seeing a q about that there a while ago. – MartynA Sep 22 '17 at 13:39
  • If I try this in the initialize procedure, i get that ViewData is not accessible: TcxGridDbTableView.ViewData.Expand(True); – AM AM Sep 22 '17 at 13:44
  • Which initialize procedure? See my answer, where I do it in the `FormCreate`. – MartynA Sep 22 '17 at 13:57
  • @AM AM, it might be a typo, but you are using `TcxGridDbTableView` - a class - and not a variable. I don't think that is your intention. – nil Sep 22 '17 at 14:13

1 Answers1

4

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

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • I'm sorry, I'm feeling quite dumb. It says that cxGrid1DBTableView1 is undeclared. My grid is a TxcGrid and in the structure is like this: cxGrid7 -> cxGrid7Level1 and dxDBGridEvents . Can you please help me with the syntax? – AM AM Sep 22 '17 at 14:00
  • @AMAM: In your cxGrid in the IDE, bottom RH corner, there should be a label like cxGrid1Level1. The name of your grid view component is to the right of it. Mine is as quoted in my code - just substitute yours. – MartynA Sep 22 '17 at 14:12
  • Thank you very much for the effort, It was correct, I just had to accept Nil's answer because for me being a total beginner, I could understand it better. Thank you – AM AM Sep 22 '17 at 14:19
  • @AMAM: No worries, you should always accept whichever answer suits you best. Anyway, goo luck with the cxGrid - some of the things it can do a re little short of amazing, but finding out how to do in code things that can so easily be done with its visual designers can be a bit of an uphill struggle. – MartynA Sep 22 '17 at 14:28
  • @MartynA actually the DataController option should do the whole trick. That was what was missing in your answer at the time I wrote mine. Would you mind rewriting yours, so that it is clear that for OPs problem it is the solution, while using ViewData.Expand(True) is perfect for expanding all data groups without interfering in end user capabilities. So, merge our answers, I would then delete mine. – nil Sep 22 '17 at 15:17
  • @Nil: Thanks. I've updated my answer along the lines you suggested. – MartynA Sep 22 '17 at 16:39
  • 1
    @AM AM would you be so kind to accept martyns answer? This contains a lot more content including the right answer that slready helped you. – nil Sep 22 '17 at 17:03