title: How do I keep the same row selected in a datagridview after refreshing the dataset?
body: <p>This code is called from a timer tick event so that the dataviewgrid refreshes at frequent intervals.</p>
  
  <p>From other answers I found on SO I would expect this code to reset the selected row to the row that was selected before this code runs, refreshing my dataset. </p>
  
  <p>Only the variable CurrentSelectedRow is a public variable, all others are local.</p>
  
  <pre><code>sql = "select top 10 batch, TrussName, PieceName from FitaPieces order by SawTime desc "
  
      myDataset = SelectFromDB(sql)
  
      MyPreviouslyCutPieces.ClearAll()
  
      Me.dgvPreviouslyCut.SelectionMode = DataGridViewSelectionMode.FullRowSelect
  
      If Not IsNothing(Me.dgvPreviouslyCut.CurrentRow) Then
          Debug.Print(Now.ToString &amp; "...Current Row = " &amp; Me.dgvPreviouslyCut.CurrentRow.ToString)
          CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index
      Else
          Debug.Print(Now.ToString &amp; "...Current Row = -1")
          CurrentSelectedRow = -1
      End If
  
      If Not myDataset Is Nothing Then
          If myDataset.Tables("CurData").Rows.Count &gt; 0 Then
  
              Me.dgvPreviouslyCut.DataSource = myDataset
              Me.dgvPreviouslyCut.DataMember = "CurData"
  
          End If
      End If
  
      If CurrentSelectedRow &lt;&gt; -1 Then
          Me.dgvPreviouslyCut.Rows(0).Selected = False
          Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True
      End If
  </code></pre>
  
  <p>And it does..for the first tick of the timer event. On the second tick event after the user selects a row, it reverts back to the first row being selected. Even though the variable CurrentSelectedRow is a public variable, it's getting reset to zero after the first tick event. Then the selected row switches back to the first row in the grid.  The first row is auto selected when you refresh a grid's datasource, but I'm setting it's selected status to false after the refresh.</p>
  
  <p>How is the dataviewgrid's selected row getting reset to the first row?</p>