title: DataGridView selection changed event and deleting selected rows from database
body: <p>I have a radiobutton, a gridview and a button on my form. Here is my <code>datagridview selection_changed</code> code sample: </p>
  
  <pre><code>private void DataGridView1_SelectionChanged(object sender, EventArgs e)
  {
      if (rdb_Delete.Checked)
      {
          lbl_deleteMsg.Text = DataGridView1.SelectedRows.Count.ToString() 
                  + " rows selected.";
      }
   }
  </code></pre>
  
  <p>I want to delete selected rows from database and refresh the datagridview when user pushes the delete button. 
  I have an Id column in my database but I don't show it to the user on datagridview. </p>
  
  <p>So by sql query how can I delete the selected row from my database? Here is my delete button clicked code but it doesn't seems to work (because of selection changed event):</p>
  
  <pre><code>private void btn_Delete_Click(object sender, EventArgs e) 
  {       
      if (rdb_Delete.Checked)
      {
          int count = DataGridView1.SelectedRows.Count;
          int length = DataGridView1.RowCount;
          if (!count.Equals(0))
          {
              if (confirm())
              {
                  for (int i = 0; i &lt; length; i++)
                  {
                      if (DataGridView1.Rows[i].Selected)
                      {
                          //DataGridView1.SelectedRows[i].Selected = false;
                          DataGridView1.Rows.RemoveAt(i);
                          i--;
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Edit : I also find a small answer maybe helps someone in such a case. DataGridView's any column can be <strong>visible = false</strong> so it'll be accessible by code such as : </p>
  
  <pre><code>int rowID = int.Parse(DataGridView1[0, selectedIndex].Value.ToString());
  </code></pre>