Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello All,
Good Evening

I am not able to delete row from database...
When i select one row from data gridview and click on delete button its get deleted but not from database...

I have wrote below code ...

C#
private void btndelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }
        }



How to update in database for deleting row??
Please Help !!!
Thank You.
Posted
Updated 26-Jun-12 0:35am
v2

 
Share this answer
 
Comments
Naksh_27 26-Jun-12 7:01am    
Hello,,

I am using Visual studio 2005 and i could not found Edit Template in that.
Please suggest how to to code for delete row from database on Button click event.
pradiprenushe 26-Jun-12 7:44am    
private void btndelete_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
Label lblID= (Label) dataGridView1.SelectedRows[0].FindControl("lblPrimaryKey");
SqlCommand cmd = new SqlCommand ("Delete from table where id='"+ Convert.ToInt32(lblID.Text)+"'",ConnectionObject);
cmd.ExecutenonQuery();
}
}

Refer this link
http://www.c-sharpcorner.com/uploadfile/sd_patel/deletedatagridrows11222005233005pm/deletedatagridrows.aspx
qwerzxcxyz 5-Jun-15 5:41am    
well I tried your code and its not working.
this ".FindControl" and "ConnectionObject"

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