How to delete row from datagridview and database
  • How can i delete whole row from database and datagridview by clicking the cell content(button inside the datagridview)

    -------->
    this is working with delete row from datagridview but how i can delete that row from database too (table name in database is "taskdetail" and columns are 
    p_title
    task_num
    task_id
    task_detail

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {

    if (dataGridView1.CurrentCell.ColumnIndex == 1)
    {

    this.dataGridView1.Rows.RemoveAt(e.RowIndex);
    MessageBox.Show("Record Deleted Succesfully");

    }
    }

    please help

  • 1 Comment sorted by
  • hi jiya,

    try this code



    if (dataGridView1.CurrentRow != null)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=DatabaseName;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "DELETE FROM taskdetail WHERE task_id=" + dataGridView1.CurrentRow.Cells["task_id"].Value.ToString() + "";
    con.Open();
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    con.Close();

    }

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