delete selected row from datagridview and database in c#
  • how can i delete selected row from datagridview and in from database table.
  • 1 Comment sorted by
  • Vote Up0Vote Down ithackerithacker
    117.50 Karma Accepted Answer
    you can use below code for delete selected row of datagridview from database.

    SqlConnection con=new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=DatabaseName;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
    delcmd.CommandText = "DELETE FROM table_Name WHERE Column_Name=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
    delcmd.Connection = con;
    MessageBox.Show("Row has been Deleted");
    //re bind grid and fill from database again

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook


Popular Posts of the Week

    Optimum Creative