ComponentOne True DBGrid for WinForms
Data Presentation Techniques / Column Grouping / Expanding and Collapsing Grouped Rows
In This Topic
    Expanding and Collapsing Grouped Rows
    In This Topic

    To expand or collapse all grouped rows at once, you can use the ExpandGroupRow and CollapseGroupRow methods. In this topic you'll add buttons to your form that will expand and collapse your grouped grid using the ExpandGroupRow and CollapseGroupRow methods.

    Complete the following steps:

    1. Start a new .NET project.
    2. Open the Toolbox and add a SplitContainer to the form.
    3. Select the SplitComntainer1's smart tag to open the SplitContainer Tasks menu and select Horizontal splitter orientation.
    4. Select SplitContainer1.Panel2, the bottom panel in the SplitContainer and navigate to the Toolbox to add 2 Button controls, Button1 and Button2, to the panel.
    5. Resize the buttons on the form, and set the Text properties for the buttons in the designer or in code:

      In the Designer

      In the Properties window set the following properties:

      • Select Button1 and in the Properties window set its Text property to "Expand".
      • Select Button2 and in the Properties window set its Text property to "Collapse".

      In Code

      Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.Button1.Text = "Expand"
      Me.Button2.Text = "Collapse"
      

      To write code in C#

      C#
      Copy Code
      this.button1.Text = "Expand";
      this.button2.Text = "Collapse";
      
    6. Select SplitContainer1.Panel1, the top panel in the SplitContainer, and navigate to the Toolbox to add a C1TrueDBGrid control to the panel.
    7. Open the C1TrueDBGrid Tasks menu and select Dock in parent container.
    8. In the C1TrueDBGrid Tasks menu, click the drop-down arrow in the Choose Data Source box, and click Add Project Data Source.
    9. In the adapter's Data Source Configuration Wizard, either select a connection to C1NWind.mdb or create a new connection to this database.
    10. On the Choose your database objects page of the wizard, select all fields in the Products table and type "Products" into the DataSet name box, and then finish out the wizard.

      Visual Studio adds the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.ProductsTableAdapter.Fill(Me.Products._Products)
      

      To write code in C#

      C#
      Copy Code
      this.productsTableAdapter.Fill(this.products._Products);
      
    11. Set the DataView property to DataViewEnum.GroupBy in the designer or in code:

      In the Designer

      In the C1TrueDBGrid Tasks menu, select GroupBy from the Data Layout drop-down.

      In Code

      Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;
      
    12. Add the following Button_Click events to the Code Editor to add the ExpandGroupRow and CollapseGroupRow methods:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Me.C1TrueDBGrid1.ExpandGroupRow(-1, True)
      End Sub
       
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          Me.C1TrueDBGrid1.CollapseGroupRow(-1)
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void button1_Click(object sender, EventArgs e)
      {
          this.c1TrueDBGrid1.ExpandGroupRow(-1, true); 
      }
       
      private void button2_Click(object sender, EventArgs e)
      {
          this.c1TrueDBGrid1.CollapseGroupRow(-1); 
      }
      

    Run the application and observe:

    1. Group the grid by dragging column headers into the GroupBy area.
    2. Select the Expand button, notice that all grouped rows and subgroups expand:
    3. Select the Collapse button, notice that all grouped rows are now collapsed:
    See Also