# How to get the current row from a DataSet after sorting its bound DataGrid

**URL:** <https://forums.thehomestack.io/t/how-to-get-the-current-row-from-a-dataset-after-sorting-its-bound-datagrid/253>\
**Category:** vb.Net\
**Created:** [May 16, 2007, 2:45pm UTC](https://forums.thehomestack.io/t/how-to-get-the-current-row-from-a-dataset-after-sorting-its-bound-datagrid/253 "2007-05-16T14:45:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [May 16, 2007, 2:45pm UTC](https://forums.thehomestack.io/t/how-to-get-the-current-row-from-a-dataset-after-sorting-its-bound-datagrid/253/1 "2007-05-16T14:45:31Z")

</div>

How to get the current row from a DataSet after sorting its bound DataGrid  
This is a common dilemma in WinForms applications. You display a DataGrid that’s bound to an underlying DataSet or DataTable. The user clicks the DataGrid’s headrow and sorts the data after they have added and deleted several rows.

In an unsorted grid you can use the following to get the current row:

```auto
    Dim dt As DataTable = DataGrid.DataSource 
    Dim dr as DataRow = dt.Rows(DataGrid.CurrentRowIndex)

```

If the grid has been sorted, you can no longer get the current row using the grid’s CurrentRowIndex property. However, for both unsorted and sorted grids, you can get the current row through the BindingContext and the Current property of the BindingManagerBase.

```auto
    Dim bm As BindingManagerBase = DataGrid.BindingContext( _
            DataGrid.DataSource, DataGrid.DataMember)
    Dim dr As DataRow = CType(bm.Current, DataRowView).Row 

```

You can also use the CurrencyManager which maps a DataGrid row to the corresponding DataSet row.

```auto
    '
    ' Specify the CurrencyManager
    '
    Dim cm As CurrencyManager = _
        CType(DataGrid.BindingContext(DataGrid.DataSource), CurrencyManager)
    Dim dr As DataRow

    '
    ' Display the current row number, get the current row and
    ' display the first column's value.
    '
    dr = myDataSet.Tables(0).Rows(cm.Position)
    Debug.WriteLine(cm.Position)
    Debug.WriteLine(dr(0))

```
