Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


            if (dataGridView1.CurrentCell.Value.ToString().Equals("Delete"))
            {
                DialogResult res = MessageBox.Show("Are you sure you want to delete the item?", "Shayona", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    conn.Open();
                    int id = Int32.Parse(dataGridView1.CurrentRow.Cells[6].Value.ToString());
                    str = "delete from branchdetails where BranchID=" + id;
                    cmd = new MySqlCommand(str, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
}
Posted
Comments
piyush_singh 3-Jul-13 8:24am    
I am assuming the Column Header that you're clicking has the text "Delete". But what I would like to know is that if you're using a button in gridview to fire the delete query then why not use the Row_Deleting event? In that way your application need not have to check on each cell click whether it's a delete event or not. The delete query will only be fired when the user clicks on the delete button. You can also put your javascript confirmation to that delete button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900