Articles in this section
Category / Section

How to display nested tables in the same level as the parent without being indented in WinForms GridGroupingControl?

2 mins read

Display nested table in same level

The child table can be displayed in the same level as the parent by hiding the default RecordPlusMinus buttons. To hide the +/- buttons, the TableOptions.ShowIndentTable property of the ChildTableDescriptor should be set to false. Then, the QueryCellStyleInfo and TableControlCellButtonClicked events have to be handled in a proper way to get this working. The following codes show how to achieve this.

Step 1: Setting the TableOptions.ShowIndent property.

C#

//Sets ShowTableIndent to false for the ChildTable.
this.gridGroupingControl1.GetTableDescriptor("ChildTable").TableOptions.ShowTableIndent = false;

VB

'Sets ShowTableIndent to false for the ChildTable.
Me.gridGroupingControl1.GetTableDescriptor("ChildTable").TableOptions.ShowTableIndent = False

Step 2: Handling the QueryCellStyleInfo event to format the PlusMinus buttons.

C#

this.gridGroupingControl1.QueryCellStyleInfo += gridGroupingControl1_QueryCellStyleInfo;    
void gridGroupingControl1_QueryCellStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs e)
{
   if(e.TableCellIdentity.TableCellType == GridTableCellType.NestedTableIndentCell)
   {
      if(e.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.NestedTable)
      {
         // NestedTable.
         GridNestedTable nt = (GridNestedTable)e.TableCellIdentity.DisplayElement;
         foreach (Element el in nt.ChildTable.Elements)
         {
            if(el.Kind == DisplayElementKind.Record)
            {
                if(e.TableCellIdentity.RowIndex == this.gridGroupingControl1.Table.NestedDisplayElements.IndexOf(el))
                {
                   Record r = Record.GetParentRecord(el);
                   e.TableCellIdentity.TableCellType = GridTableCellType.RecordPlusMinusCell;
                   e.Style.Description = r.IsExpanded ? "-" : "+";      
                   e.Style.BorderMargins = new Syncfusion.Windows.Forms.Grid.GridMarginsInfo(3, 3, 3, 3);
                   e.Style.Themed = false;
                   e.Style.Enabled = true;
                }
            }
         }
      }
   }
}

VB

Private Me.gridGroupingControl1.QueryCellStyleInfo += AddressOf gridGroupingControl1_QueryCellStyleInfo
Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs)
   If e.TableCellIdentity.TableCellType = GridTableCellType.NestedTableIndentCell Then
      If e.TableCellIdentity.DisplayElement.Kind = DisplayElementKind.NestedTable Then
         'NestedTable.
         Dim nt As GridNestedTable = CType(e.TableCellIdentity.DisplayElement, GridNestedTable)
         For Each el As Element In nt.ChildTable.Elements
            If el.Kind Is DisplayElementKind.Record Then
     If e.TableCellIdentity.RowIndex = Me.gridGroupingControl1.Table.NestedDisplayElements.IndexOf(el) Then
        Dim r As Record = Record.GetParentRecord(el)
        e.TableCellIdentity.TableCellType = GridTableCellType.RecordPlusMinusCell
        e.Style.Description = If(r.IsExpanded, "-", "+")
        e.Style.BorderMargins = New Syncfusion.Windows.Forms.Grid.GridMarginsInfo(3, 3, 3, 3)
        e.Style.Themed = False
        e.Style.Enabled = True
     End If
  End If
         Next el
      End If
   End If
End Sub

Step 3: The TableControlCellButtonClicked event is responsible for expanding/collapsing the record when its RecordPlusMinus button is pressed.

C#

this.gridGroupingControl1.TableControlCellButtonClicked += gridGroupingControl1_TableControlCellButtonClicked.          
//Expands the Records on the plus/Minus button Click.
void gridGroupingControl1_TableControlCellButtonClicked(object sender, GridTableControlCellButtonClickedEventArgs e)
{
   GridTableCellStyleInfo style = this.gridGroupingControl1.Table.GetTableCellStyle(e.Inner.RowIndex, e.Inner.ColIndex);
   if(style.TableCellIdentity == null)
      return;
   if(style.TableCellIdentity.TableCellType == GridTableCellType.RecordPlusMinusCell)
   {
      if(style.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.NestedTable)
      {
         //NestedTable.
         GridNestedTable nt = (GridNestedTable)style.TableCellIdentity.DisplayElement;
         foreach (Element el in nt.ChildTable.Elements)
         {
            if(el.Kind == DisplayElementKind.Record)
            {
               if(style.TableCellIdentity.RowIndex == this.gridGroupingControl1.Table.NestedDisplayElements.IndexOf(el))
               {
                  Record r = Record.GetParentRecord(el);
                  bool shouldExpand = !r.IsExpanded;
                  this.gridGroupingControl1.Table.CurrentRecord = r;
                  r.IsExpanded = shouldExpand;
                  e.Inner.Cancel = true;
               }
            }
         }
      }
   }
}

VB

Private Me.gridGroupingControl1.TableControlCellButtonClicked += AddressOf gridGroupingControl1_TableControlCellButtonClicked
'Expands the Records on the plus/Minus button Click.
Private Sub gridGroupingControl1_TableControlCellButtonClicked(ByVal sender As Object, ByVal e As GridTableControlCellButtonClickedEventArgs)
   Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.Table.GetTableCellStyle(e.Inner.RowIndex, e.Inner.ColIndex)
   If style.TableCellIdentity Is Nothing Then
      Return
   End If
   If style.TableCellIdentity.TableCellType = GridTableCellType.RecordPlusMinusCell Then
      If style.TableCellIdentity.DisplayElement.Kind = DisplayElementKind.NestedTable Then
         'NestedTable.
         Dim nt As GridNestedTable = CType(style.TableCellIdentity.DisplayElement, GridNestedTable)
         For Each el As Element In nt.ChildTable.Elements
   If el.Kind Is DisplayElementKind.Record Then
      If style.TableCellIdentity.RowIndex = Me.gridGroupingControl1.Table.NestedDisplayElements.IndexOf(el) Then
         Dim r As Record = Record.GetParentRecord(el)
         Dim shouldExpand As Boolean = Not r.IsExpanded
         Me.gridGroupingControl1.Table.CurrentRecord = r
         r.IsExpanded = shouldExpand
         e.Inner.Cancel = True
      End If
   End If
         Next el
      End If
   End If
End Sub

Show the nested table in the same level

Samples:

C#: DisplayNestedTableInSameLevel-C#1694527386.zip

VB: DisplayNestedTableInSameLevel-VB2052906042.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied