Category / Section
How to get the actual row index even after applying the filtering or sorting in WinForms GridGroupingControl?
1 min read
Get row index after filtering or sorting
By default, the rowindex can be fetched from the QuerCellStyleInfo event, but that changes when filtering or sorting is applied.
Solution:
To get the actual rowindex after filtering and sorting, you can fetch it from the class Record. In the Record class, the ID property returns the exact rowindex whether filtering/sorting is applied or not. The following code sample displays how after applying the filter, the actual rowindex can be obtained when one cell is clicked.
C#
//Cellclick event hooked in the constructor. this.gridGroupingControl1.TableControlCellClick += new GridTableControlCellClickEventHandler(gridGroupingControl1_TableControlCellClick); //Cellclick event handled. void gridGroupingControl1_TableControlCellClick(object sender, GridTableControlCellClickEventArgs e) { GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex); GridTableCellStyleInfoIdentity id = style.TableCellIdentity; if(id.DisplayElement.Kind== DisplayElementKind.Record) { Record rec = id.DisplayElement.GetRecord(); MessageBox.Show("RowIndex is:" + rec.Id.ToString()); //Real RowIndex } }
VB
'Cellclick event hooked in constructor. Me.gridGroupingControl1.TableControlCellClick += New GridTableControlCellClickEventHandler (gridGroupingControl1_TableControlCellClick) 'Cellclick event handled. Private Sub gridGroupingControl1_TableControlCellClick(sender As Object, e As GridTableControlCellClickEventArgs) Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex) Dim id As GridTableCellStyleInfoIdentity = style.TableCellIdentity If id.DisplayElement.Kind = DisplayElementKind.Record Then Dim rec As Record = id.DisplayElement.GetRecord() 'Real RowIndex. MessageBox.Show("RowIndex is:" + rec.Id.ToString()) End If End Sub
The following screenshot displays the RowIndex after Filtering or Sorting: