Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to delete a row in gridview using entity framework 4.0. The row is not deleting and this is my code:

C#
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
    tbl_emp objempdelete = new tbl_emp();

    //int empId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
    foreach (GridViewRow row in GridView1.Rows)
    {
        int empId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

        var deleteQuery = from objsel in objLMS.tbl_emp
                                          where (objsel.empid == empId)
                                          select objsel;
        objLMS.DeleteObject(deleteQuery);
        objLMS.SaveChanges();

    }
    BindData();
}


Thanks for your attention!
Posted
Updated 18-Oct-11 23:05pm
v6

1 solution

We'll need more information than you have provided.

If you need to delete a row in the entity framework data model, it's like this:

C#
MyModel model = ;// your model.
model.DeleteOject(myEntity); // the entity you want to delete
model.SaveChanges(); // commit the changes


Now when you say grid view, are you talking about the grid view mode of a list view? If so, you should be binding the ItemsSource to an observable collection of your entities. Then you can remove an entity from the observable collection and the grid will automatically be updated.
 
Share this answer
 
Comments
jayanthik 19-Oct-11 4:36am    
see this

protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
tbl_emp objempdelete = new tbl_emp();

//int empId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
foreach (GridViewRow row in GridView1.Rows)
{
int empId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

var deleteQuery = from objsel in objLMS.tbl_emp
where (objsel.empid == empId)
select objsel;
objLMS.DeleteObject(deleteQuery);
objLMS.SaveChanges();

}
BindData();
}

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