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() + "";
    con.Open();
    delcmd.Connection = con;
    delcmd.ExecuteNonQuery();
    con.Close();
    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
    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

Tagged

Popular Posts of the Week

    Optimum Creative