How to access the corresponding parent table's row from a child table's record in WinForms GridGroupingControl?
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.
//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();
}
}
}'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 SubThe following
screenshot illustrates the output.

Figure 1: Accessing Parent Table Rows using Child Table Records
Samples: