We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Create Hierarchy view of single table with parent and Child properties

I am trying yo get the GridGropingControl to display data from a single table that has recursive data. The data has an ID field and a ParentID field. I want all of the records that do not have a ParentID value to display in the control and then be able to open up the Parent record and see all of the items that have a ParentID value equal to the record I just opened. 

For example
Name     ID     Parent ID
Test 1      1      
Test 2      2       1
Test 3      3    
Test 4      4       3
Test 5      5

In the sample data above the control would display 2 lines to start ( Test 1 and Test 3)
If I open the related data for Test 1 I would see Test 2.

I have tries to create a MasterDetail record on the same table that the control is linked to but the related table and the design view shows the same table as the related table. But when I run the form and open up the related table there are no related records. My data is setup with some relations that should show a child record for some of the parent records. does not have any related table ( Test 5 in the data above) ?

Also, how can I remove the "+" symbol if the master record does 

3 Replies

AA Arulraj A Syncfusion Team January 14, 2019 06:07 AM

Hi George, 

Thanks for using Syncfusion product. 

I have tries to create a MasterDetail record on the same table that the control is linked to but the related table and the design view shows the same table as the related table. But when I run the form and open up the related table there are no related records. My data is setup with some relations that should show a child record for some of the parent records 
To create a child table for same table, you could prepare the separate table to create the relation between that two tables. Please refer the following code example. 

DataTable parentTable = CreateTable(); 
DataTable child = CreatedchildTable(); 
child.TableName = "ParetToChild"; 
GridRelationDescriptor relation = new GridRelationDescriptor(); 
relation.ChildTableName = "Relation"; 
relation.RelationKind = Syncfusion.Grouping.RelationKind.RelatedMasterDetails; 
relation.RelationKeys.Add("Id", "ParentId"); 
this.gridGroupingControl1.SourceListSet.Add("Parent", parentTable); 
this.gridGroupingControl1.SourceListSet.Add("Relation", child); 
this.gridGroupingControl1.DataSource = parentTable; 
how can I remove the "+" symbol if the master record does  
To hide the caption plus/minus cell, you could set the CellType as Static for RecordPluseMinusCell in QueryCellStyleInfo event. Please refer the following code example. 

private void GridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
    if (e.TableCellIdentity.TableCellType == GridTableCellType.RecordPlusMinusCell) 
        Element cell = e.TableCellIdentity.DisplayElement; 
        Record r = cell.ParentRecord as Record; 
        bool makeStatic = true; 
        if (r != null && r.NestedTables.Count > 0) 
            foreach (NestedTable nt in r.NestedTables) 
                if (nt.ChildTable.GetFilteredRecordCount() != 0) 
                    makeStatic = false; 
        if (makeStatic) 
            e.Style.CellType = "Static"; 
            e.Style.Borders.Bottom = new GridBorder(GridBorderStyle.Solid, Color.FromArgb(208, 215, 229)); 

Please let us know if you have any concerns. 

Arulraj A 

GB George Busby January 19, 2019 03:41 PM

I do not want to setup this capability in code unless I have to. I am using the Properties editor to setup the relation. Below is the code that was auto generated for my control when I used the properties window to setup the relation. this is not working as it does not display any related records. Can you let me know what I am doing wrong in the properties editor and not using code, unless that is the only way to get this to work.

Here is the auto generated code:
  Me.grdProject.AlphaBlendSelectionColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(120, Byte), Integer), CType(CType(215, Byte), Integer))
        Me.grdProject.Appearance.FilterBarCell.CellType = "FilterByDisplayMemberCell"
        Me.grdProject.AutoPopulateRelations = False
        Me.grdProject.BackColor = System.Drawing.SystemColors.Window
        Me.grdProject.DataSource = Me.ProjectBindingSource
        Me.grdProject.Dock = System.Windows.Forms.DockStyle.Top
        Me.grdProject.GridVisualStyles = Syncfusion.Windows.Forms.GridVisualStyles.Office2010Blue
        Me.grdProject.Location = New System.Drawing.Point(0, 31)
        Me.grdProject.Name = "grdProject"
        Me.grdProject.NestedTableGroupOptions.ShowFilterBar = True
        Me.grdProject.ShowCurrentCellBorderBehavior = Syncfusion.Windows.Forms.Grid.GridShowCurrentCellBorder.GrayWhenLostFocus
        Me.grdProject.ShowGroupDropArea = True
        Me.grdProject.Size = New System.Drawing.Size(900, 220)
        Me.grdProject.TabIndex = 0
        GridColumnDescriptor1.AllowFilter = True
        GridColumnDescriptor1.Appearance.FilterBarCell.CellType = ""
        GridColumnDescriptor1.MappingName = "ProjectName"
        GridColumnDescriptor2.AllowFilter = True
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.AutoSize = True
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.CellType = "ComboBox"
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.DataSource = Me.ProjectStatusBindingSource
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.DisplayMember = "Status"
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.DropDownStyle = Syncfusion.Windows.Forms.Grid.GridDropDownStyle.AutoComplete
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.TriState = True
        GridColumnDescriptor2.Appearance.AnyRecordFieldCell.ValueMember = "ProjectStatusID"
        GridColumnDescriptor2.Appearance.ColumnHeaderCell.CellType = "GridExcelFilterCell"
        GridColumnDescriptor2.Appearance.ColumnHeaderCell.DataSource = Me.ProjectStatusBindingSource
        GridColumnDescriptor2.Appearance.ColumnHeaderCell.DisplayMember = "Status"
        GridColumnDescriptor2.Appearance.ColumnHeaderCell.ValueMember = "ProjectStatusID"
        GridColumnDescriptor2.Appearance.ColumnHeaderWithFilterCell.AllowEnter = True
        GridColumnDescriptor2.Appearance.FilterBarCell.CellType = "ComboBox"
        GridColumnDescriptor2.Appearance.FilterBarCell.CellValueType = Nothing
        GridColumnDescriptor2.HeaderText = "Project Status"
        GridColumnDescriptor2.MappingName = "ProjectStatusID"
        GridColumnDescriptor2.SortByDisplayMember = True
        GridColumnDescriptor3.AllowFilter = True
        GridColumnDescriptor3.MappingName = "ProjectStageID"
        GridColumnDescriptor4.AllowFilter = True
        GridColumnDescriptor4.MappingName = "ProjectManager"
        GridColumnDescriptor5.AllowFilter = True
        GridColumnDescriptor5.MappingName = "StartDate"
        GridColumnDescriptor6.AllowFilter = True
        GridColumnDescriptor6.MappingName = "CompletionDate"
        Me.grdProject.TableDescriptor.Columns.AddRange(New Syncfusion.Windows.Forms.Grid.Grouping.GridColumnDescriptor() {GridColumnDescriptor1, GridColumnDescriptor2, GridColumnDescriptor3, GridColumnDescriptor4, GridColumnDescriptor5, GridColumnDescriptor6, New Syncfusion.Windows.Forms.Grid.Grouping.GridColumnDescriptor("ProjectDescription"), New Syncfusion.Windows.Forms.Grid.Grouping.GridColumnDescriptor("ProjectShortDescription")})
        GridRelationDescriptor1.ChildTableName = "Project"
        GridRelationDescriptor1.Name = "ChildProject"
        RelationKeyDescriptor1.ChildKeyFieldName = "ProjectId"
        RelationKeyDescriptor1.ParentKeyFieldName = "ParentProjectID"
        GridRelationDescriptor1.RelationKeys.AddRange(New Syncfusion.Grouping.RelationKeyDescriptor() {RelationKeyDescriptor1})
        Me.grdProject.TableDescriptor.Relations.AddRange(New Syncfusion.Windows.Forms.Grid.Grouping.GridRelationDescriptor() {GridRelationDescriptor1})
        Me.grdProject.TableOptions.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Row
        Me.grdProject.Text = "Project"
        Me.grdProject.UseRightToLeftCompatibleTextBox = True
        Me.grdProject.VersionInfo = "16.4460.0.42"

AA Arulraj A Syncfusion Team January 21, 2019 06:17 AM

Hi George, 
Thanks for your update. 
To create the relation in GridGroupingControl, your binding source(ProjectBindingSource) should have a relation otherwise you could create a relation for your binding source in designer level using dataset. So, please ensure that in your application. Please refer the following sample. 
Please let us know if you have any concerns. 
In above attached sample, binding source is DataTable(dataSet11.Customers) and relation has created in DataSet1.Designer InitClass method. 
Arulraj A 

Live Chat Icon For mobile
Up arrow icon