Category / Section
How to maintain the selection after grid is refreshed?
2 mins read
To maintain the selection after refreshing the grid, use the SelectionChanging and MouseUp events. In the SelectionChanging event, the cell value of a particular column in the selected record is obtained by using the Record.GetValue() method. In the MouseUp event, the selection is restored by looping through the records to find the record value obtained in the SelectionChanging event.
Code Snippet
CS
//Event triggering this.gridDataBoundGrid1.Model.SelectionChanging += Model_SelectionChanging; this.gridDataBoundGrid1.MouseUp += GridDataBoundGrid1_MouseUp; //Event customization private void Model_SelectionChanging(object sender, GridSelectionChangingEventArgs e) { ranges = this.gridDataBoundGrid1.Selections.GetSelectedRows(true, false); foreach (GridRangeInfo range in ranges) { //To get the Record record = gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(range.Top); GridBoundColumn column = this.gridDataBoundGrid1.Binder.InternalColumns["ID"]; //To get the value of ‘ID’ column selectedCellValue = Convert.ToInt16(column.PropertyDescriptor.GetValue(record.Table[record.Position]).ToString()); } } private void GridDataBoundGrid1_MouseUp(object sender, MouseEventArgs e) { int rowIndex = -1; int colIndex = -1; this.gridDataBoundGrid1.PointToRowCol(e.Location, out rowIndex, out colIndex); if(rowIndex == 0) { for (int i = 1; i <= this.gridDataBoundGrid1.Binder.RecordCount; i++) { //To get the Record GridBoundRecordState changedRecord = this.gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(i); GridBoundColumn column = this.gridDataBoundGrid1.Binder.InternalColumns["ID"]; //To get the particular cell value int cellVal = Convert.ToInt16(column.PropertyDescriptor.GetValue(changedRecord.Table[changedRecord.Position])); if (cellVal == selectedCellValue) //To add the selection gridDataBoundGrid1.DataBoundGridModel.Selections.Add(GridRangeInfo.Row(i)); } } }
VB
'Event triggering AddHandler Me.gridDataBoundGrid1.Model.SelectionChanging, AddressOf Model_SelectionChanging AddHandler Me.gridDataBoundGrid1.MouseUp, AddressOf GridDataBoundGrid1_MouseUp 'Event customization Private Sub Model_SelectionChanging(ByVal sender As Object, ByVal e As GridSelectionChangingEventArgs) ranges = Me.gridDataBoundGrid1.Selections.GetSelectedRows(True, False) For Each range As GridRangeInfo In ranges 'To get the Record record = gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(range.Top) Dim column As GridBoundColumn = Me.gridDataBoundGrid1.Binder.InternalColumns("ID") 'To get the particular id value selectedCellValue = Convert.ToInt16(column.PropertyDescriptor.GetValue(record.Table(record.Position)).ToString()) Next range End Sub Private Sub GridDataBoundGrid1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Dim rowIndex As Integer = -1 Dim colIndex As Integer = -1 Me.gridDataBoundGrid1.PointToRowCol(e.Location, rowIndex, colIndex) If rowIndex = 0 Then For i As Integer = 1 To Me.gridDataBoundGrid1.Binder.RecordCount 'To get the Record Dim changedRecord As GridBoundRecordState = Me.gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(i) Dim column As GridBoundColumn = Me.gridDataBoundGrid1.Binder.InternalColumns("ID") 'To get the particular cell value Dim cellVal As Integer = Convert.ToInt16(column.PropertyDescriptor.GetValue(changedRecord.Table(changedRecord.Position))) If cellVal = selectedCellValue Then 'To add the selection gridDataBoundGrid1.DataBoundGridModel.Selections.Add(GridRangeInfo.Row(i)) End If Next i End If End Sub
Sample Link:
CS: Maintain grid selections_CS
VB: Maintain grid selections_VB