title: SelectedIndexChanged EventOutOfRange Exception when clicking datagridview headers in C#
body: <p>I am having an issue with my dataGridView that causes an EventOutOfRange Exception error when the column headers of the view are clicked. I understand why the error is happening, I am just having an issue creating a work around. Is there a way I can create an if statement that looks to see if the selected index is -1? Everything I have tried is invalid.</p>
  
  <pre><code>    private void student_grid_SelectionChanged(object sender, EventArgs e)
      {
          DataGridView theGrid = sender as DataGridView;
          if (theGrid != null)
          {
              //The line below is what causes the error.
              DataGridViewRow row = theGrid.SelectedRows[0];
              stuinfo_fill.Text = row.Cells["Student ID"].Value.ToString();    
          }
          if (stuinfo_fill.Text == String.Empty)
          {
              MessageBox.Show("You must have a student selected.");
          }
          else
          {
              PhysicianFill(stuinfo_fill.Text);
              StudentInfo(stuinfo_fill.Text);
              sch_rad.Checked = false;
              newsch_btn.Enabled = true;
              schList_btn.Enabled = true;
              phynew_btn.Enabled = true;
              phylist_btn.Enabled = true;
  
          }
      }
  </code></pre>