Articles in this section
Category / Section

How to deselect the rows in the grid programmatically?

2 mins read

In GridDataBoundGrid selection can be cleared by using Selection.Clear() method. The Clear method of ranges is used to remove the collection along from the Ranges collection where the Selection.Clear() is used to remove all selection from Grid.

private void button1_Click(object sender, EventArgs e)
{
    this.gridDataBoundGrid1.SortColumn(1);
 
    // To Remove the selection from Grid
    this.gridDataBoundGrid1.Selections.Clear();
} 

 

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
 Me.gridDataBoundGrid1.SortColumn(1)
 
 ' To Remove the selection from Grid
 Me.gridDataBoundGrid1.Selections.Clear()
End Sub

 

While sorting the column using cell click on header cell, Current cell is moved to first cell if grid doesn’t has any current cell. Due to this, selection also moved to the first row. You can avoid this issue by using CurrentCellMoving event.

 

this.gridDataBoundGrid1.CurrentCellMoving += gridDataBoundGrid1_CurrentCellMoving;

 

void gridDataBoundGrid1_CurrentCellMoving(object sender, GridCurrentCellMovingEventArgs e)

{

    if (e.RowIndex == 1)

    {

        e.Cancel = true;

    }

}

 

Private Me.gridDataBoundGrid1.CurrentCellMoving += AddressOf gridDataBoundGrid1_CurrentCellMoving
 
Private Sub gridDataBoundGrid1_CurrentCellMoving(ByVal sender As Object, ByVal e As GridCurrentCellMovingEventArgs)
 If e.RowIndex = 1 Then
  e.Cancel = True
 End If
End Sub

 

Sample

GridDataBoundGridSelection_C#

GridDataBoundGridSelection_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied