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..
- SqlCommand delcmd = new SqlCommand();
- if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
- {
- delcmd.CommandText = "DELETE FROM StudentRegistration WHERE Rollno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
- //con.Open();
- delcmd.Connection = con;
- delcmd.ExecuteNonQuery();
- //con.Close();
- dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
- MessageBox.Show("Row has been Deleted");
- }

Amit Kumar SinghRank #562 · 4.3K reputationPosted Jan 7, 2017, 3:26 AM EST
Chetan RanpariyaRank #1,852 · 801 reputationPosted Jan 7, 2017, 3:28 AM EST
Bikesh SrivastavaRank #108 · 20.3K reputationPosted Jan 7, 2017, 5:52 AM EST