)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to access the corresponding parent table's row from a child table's record in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: April 30, 2011 |
Last Revised Date: February 13, 2020

Accessing parent table rows using child table record

To access the parent table’s row from the child table record, get the table's current element. Then, cast that element to the GridNestedTable. Now, you can access the parent table for the current element.

C#

//Hook the button Click event in Form_Load
this.btnGetRecord.Click += new System.EventHandler(this.btnGetRecord_Click);
private void btnGetRecord_Click(object sender, EventArgs e)
{
   //Get the current element from the TableControl
   Element el = this.gridGroupingControl1.TableControl.Table.CurrentElement;
   if(el != null)
   {
      if(el is GridRecord)
      {
         Console.WriteLine("no parent row...");
         textBox1.Text = "no parent row...";
      }
      else if(el is GridNestedTable)
      {
         //Get the element as the nested table
         GridNestedTable gridNestedTable = el as GridNestedTable;
         GridNestedTable gridNestedTable1 = gridNestedTable;
         //Loop untill get the parent record of the current element
         while (gridNestedTable1 != null && gridNestedTable1.ChildTable != null)
         {
             gridNestedTable = gridNestedTable1;
             gridNestedTable1 = gridNestedTable.ChildTable.ParentTable.CurrentElement as      GridNestedTable;
         }
         DataRowView dataRowView = gridNestedTable.ParentRecord.GetData() as DataRowView;
         //Write the Column 2
         Console.WriteLine(dataRowView[1].ToString());
         //show column 2
         textBox1.Text = dataRowView[1].ToString();
      }
   }
}

VB

'Hook the button Click event in Form_Load
AddHandler btnGetRecord.Click, AddressOf btnGetRecord_Click
Private Sub btnGetRecord_Click(ByVal sender As Object, ByVal e As EventArgs)
   'Get the current element from the TableControl
   Dim el As Element = Me.gridGroupingControl1.TableControl.Table.CurrentElement
   If el IsNot Nothing Then
      If TypeOf el Is GridRecord Then
         Console.WriteLine("no parent row...")
         textBox1.Text = "no parent row..."
      ElseIf TypeOf el Is GridNestedTable Then
         'Get the element as the nested table
         Dim gridNestedTable As GridNestedTable = TryCast(el, GridNestedTable)
         Dim gridNestedTable1 As GridNestedTable = gridNestedTable
         'Loop untill get the parent record of the current element
         Do While gridNestedTable1 IsNot Nothing AndAlso gridNestedTable1.ChildTable IsNotNothing
              gridNestedTable = gridNestedTable1
              gridNestedTable1 = TryCast(gridNestedTable.ChildTable.ParentTable.CurrentElement, GridNestedTable)
         Loop
         Dim dataRowView As DataRowView = TryCast(gridNestedTable.ParentRecord.GetData(), DataRowView)
         'Write the Column 2
         Console.WriteLine(dataRowView(1).ToString())
         'show column 2
         textBox1.Text = dataRowView(1).ToString()
      End If
   End If
End Sub

The following screenshot illustrates the output.

Accessing parent table rows using child table records

Figure 1: Accessing Parent Table Rows using Child Table Records

Samples:

C#: AccessParentTableRecords

VB: AccessParentTableRecords

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon