WinForms Grid - How to keep all Groups Expanded

Transcription

WinForms Grid - How to keep all Groups Expanded
WinForms Grid - How to keep all Groups Expanded
In this video, you will learn how to keep all the groups expanded within a Gridview. Additionally, I will
demonstrate how to prevent the groups from being collapse by the user at runtime.
1. I’ll start with a new WinForms Application.
2. I drop a Grid Control and make it fill the entire form’s area.
3. I open the grid’s smart tag and invoke the “Data Source Configuration Wizard”.
4. I’ll leave “Database” as the data source type and click on “Next”.
5. I’ll use an existing connection to the CarsDB Sample Database.
6. For this demo, I’ll select specific fields of the “Cars” table to supply data to the grid.
7. And I’m done!
8. I run the Grid’s Designer and switch to the “Columns” page.
9. I select the unnecessary columns and remove them.
10. I close the designer and return to Visual Studio.
11. First, I’m going to prevent the user from collapsing groups.
12. To do this, I’ll create a handler for the “GroupRowCollapsing” event.
13. This event is fired when an attempt is made to collapse a group row.
14. Here, I will set the “Allow” property to “False”.
e.Allow = false;
15. I switch back to design view.
16. Next, I’m going to hide the expand and collapse group row buttons.
17. This can be done using the “CustomDrawGroupRow” Event.
Copyright © 2008 Developer Express Inc
Learn more at devexpress.com
ALL RIGHTS RESERVED
18. I create a handler for it . . .
19. . . . and add the following code.
GridView view = sender as GridView;
GridGroupRowInfo groupRowInfo = e.Info as GridGroupRowInfo;
Rectangle groupRowBounds = groupRowInfo.DataBounds;
Rectangle expandButtonBounds = groupRowInfo.ButtonBounds;
Rectangle textBounds = e.Bounds;
textBounds.X = expandButtonBounds.Right + 4;
Brush brush = e.Appearance.GetBackBrush(e.Cache);
//Brushes to draw the text in the group row
Brush brushText = e.Appearance.GetForeBrush(e.Cache);
//Fill the rectangle of the group row without the expand button
e.Graphics.FillRectangle(brush, groupRowBounds);
//Draw the group row text
string s = view.GetGroupRowDisplayText(e.RowHandle);
e.Appearance.DrawString(e.Cache, s, textBounds, brushText);
//Prevent default painting
e.Handled = true;
20. Finally, we need to automatically expand all group rows when grouping is applied to the data.
21. For this, I’m going to handle the “EndGrouping” event.
22. This event is fired when data has been successfully grouped.
23. Here, I will call the “ExpandAllGroups” method of the grid view.
(sender as DevExpress.XtraGrid.Views.Grid.GridView).ExpandAllGroups();
24. And I’m done!
25. I run the application to see the results.
26. I group the data by a specific column, and you can see that all the groups are expanded
automatically.
Copyright © 2008 Developer Express Inc
Learn more at devexpress.com
ALL RIGHTS RESERVED
27. Furthermore, the expand and collapse buttons are no longer visible on the group header rows,
thus preventing the user from collapsing the groups within the grid.
For more information, please refer to the XtraGrid’s Documentation
(http://www.devexpress.com/Help/?document=XtraGrid).
Thanks for watching and thank you for choosing DevExpress!
Copyright © 2008 Developer Express Inc
Learn more at devexpress.com
ALL RIGHTS RESERVED