Category / Section
How to get the record and column of the current cell when I have nested tables in the WinForms GridGroupingControl?
2 mins read
By default, the CurrentRecord and CurrentCell returns only the parent table elements even though the current cell is in the child table. To get the nested element directly, access the following properties of the CurrentElement. By using the Table and TableControl properties of the GridGroupingControl, you can get the CurrentCell even when it is in the child table. With the CurrentCell, you can get the RowIndex and ColumnIndex and through that you could get the style info of the current cell or current record or the column name.
C#
void btnGetrcd_Click(object sender, EventArgs e) { Element el = this.gridGroupingControl1.Table.GetInnerMostCurrentElement(); if(el != null) { GridTable table = el.ParentTable as GridTable; GridTableControl tableControl = this.gridGroupingControl1.GetTableControl (table.TableDescriptor.Name); GridCurrentCell cc = tableControl.CurrentCell; GridTableCellStyleInfo style = table.GetTableCellStyle(cc.RowIndex, cc.ColIndex); GridRecord rec = el as GridRecord; if(rec == null && el is GridRecordRow) { rec = el.ParentRecord as GridRecord; } if(rec != null) { Console.WriteLine(style.TableCellIdentity.Column.Name); Console.WriteLine(rec.GetValue(style.TableCellIdentity.Column.Name)); } } }
VB
Private Sub btnGetrcd_Click(ByVal sender As Object, ByVal e As EventArgs) Dim el As Element = Me.gridGroupingControl1.Table.GetInnerMostCurrentElement() If el IsNot Nothing Then Dim table As GridTable = TryCast(el.ParentTable, GridTable) Dim tableControl As GridTableControl = Me.gridGroupingControl1.GetTableControl (table.TableDescriptor.Name) Dim cc As GridCurrentCell = tableControl.CurrentCell Dim style As GridTableCellStyleInfo = table.GetTableCellStyle(cc.RowIndex, cc.ColIndex) Dim rec As GridRecord = TryCast(el, GridRecord) If rec Is Nothing AndAlso TypeOf el Is GridRecordRow Then rec = TryCast(el.ParentRecord, GridRecord) End If If rec IsNot Nothing Then Console.WriteLine(style.TableCellIdentity.Column.Name) Console.WriteLine(rec.GetValue(style.TableCellIdentity.Column.Name)) End If End If End Sub
Figure 1: Record and Column name displayed on button click
Samples: