title: DataGridView - Index was out of range.
body: <p>Before anyone classes this as a duplicate, I have indeed searched both around StackOverflow and the Google for the solution to my problem, neither have proven to be successful.</p>
  
  <p>To summarise: I am building a game as a part of a unit in my CS course "Object Oriented Programming". The aim of the game is to randomly generate planes, who's fuel level deducts using a timer and then events such as crash are fired upon the fuel reaching 0. We then have to land the planes and assign them runways, and so on... </p>
  
  <p>This all works fine, however, I am at the stage where I have to list all the planes that are being generated and then give the user the option to land them.</p>
  
  <p>I initially used a ListView but quickly switched to a DataGridView because I wanted to have a button as a column.</p>
  
  <p>So, intially as the planes were being generated, and furthermore displayed within the DataGridView, whenever I clicked the "Land Plane" button within a row of my choosing the selection would simply jump back to the first row. I asked my lecturer for assitance, and he stated that it's because the fuel's value is constantly being updated as it counts down. When the DataGridView is cleared, the selected row resets. </p>
  
  <p>To resolve this, we added two local variables to store the selected row and the selected column. We checked to see if the selected column still remained within the collection, when the "Land Plane" button was clicked. This is because when the fuel reaches 0 it simply removes the row from the DataGridView.</p>
  
  <p>The problem I am now having, which my lecturer was puzzled by was this:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.ArgumentOutOfRangeException'
    occurred in mscorlib.dll</p>
    
    <p>Additional information: Index was out of range. Must be non-negative
    and less than the size of the collection.</p>
  </blockquote>
  
  <p>The code that handles the DataGridView is as follows:</p>
  
  <pre><code>Public Sub populateDataGV()
      Dim p As New Aircraft
  
      selRow = -1
      'Populate the data grid view
      If Not (IsNothing(DataGridView1.CurrentCell)) Then
          selRow = DataGridView1.CurrentCell.RowIndex
          selCol = DataGridView1.CurrentCell.ColumnIndex
      End If
  
  
      DataGridView1.Rows.Clear()
      DataGridView1.ColumnCount = 2
      DataGridView1.Columns(0).Name = "Flight No"
      DataGridView1.Columns(1).Name = "Fuel"
  
      For Each p In airport.planeCollection
          Dim row As String() = New String() {p.name, p.getFuelLevel()}
          DataGridView1.Rows.Add(row)
      Next
  
      Dim RowsToDelete As New List(Of DataGridViewRow)()
      For Each rows As DataGridViewRow In DataGridView1.Rows
          If rows.Cells(1).Value IsNot Nothing AndAlso rows.Cells(1).Value.ToString = "0" Then
              RowsToDelete.Add(rows)
              If selRow = rows.Index Then
                  selRow = -1
              End If
          End If
      Next
      For Each rows As DataGridViewRow In RowsToDelete
          DataGridView1.Rows.Remove(rows)
      Next
      RowsToDelete.Clear()
  
      If selRow &lt;&gt; -1 And selRow &lt;= DataGridView1.Rows.Count - 1 Then
          DataGridView1.CurrentCell = DataGridView1.Rows(selRow).Cells(selCol)
      End If
  
  
      'Add button column
      Dim btn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
      btn.HeaderText = "Action"
      btn.Text = "Land Plane"
      btn.UseColumnTextForButtonValue = True
      DataGridView1.Columns.Add(btn)
  
  
  End Sub
  </code></pre>
  
  <p>This is where the error is thrown:</p>
  
  <pre><code>If selRow &lt;&gt; -1 And selRow &lt;= DataGridView1.Rows.Count - 1 Then
          DataGridView1.CurrentCell = DataGridView1.Rows(selRow).Cells(selCol)
      End If
  </code></pre>
  
  <p>If anyone can shed some light on what is actually causing this error I'd be most grateful.</p>