Ajit N

Ajit N

  • 1k
  • 352
  • 68.2k

How To Delete Multiple Selected Rows of Datagridview ?

Jan 7 2017 5:46 AM
Hi, I want to delete selected rows of datagridview and from database also.
So, I tried following code,  but only one row are deleted at one time in datagridview and also selected row are not delete from database.
 so, plz help me..
This is my code..
 
  1. SqlCommand delcmd = new SqlCommand();  
  2.             if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)  
  3.             {  
  4.                 delcmd.CommandText = "DELETE FROM StudentRegistration WHERE Rollno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";  
  5.                 //con.Open();  
  6.                 delcmd.Connection = con;  
  7.                 delcmd.ExecuteNonQuery();  
  8.                 //con.Close();  
  9.                 dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);  
  10.                 MessageBox.Show("Row has been Deleted");  
  11.                   
  12.             }  


Answers (3)