I have bound my datagrid to a list. I am following Repository pattern and using EF v4.1. I need to delete the Entity on row_deleting event. This is the code:
protected void grdBooks_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int bookId = (int)e.Keys[0];
//grdBooks.Rows[e.RowIndex] //this item's dataItem is always null.
}
As I am working with Entities, I need the actual Entity to pass it to my GenericRepository which will delete that entity. I did get the bookId but I don’t want to do silly things like fetching the Entity from database using this bookId and then passing it to my delete method. Why is the DataItem always null and what can I do to get back my entity?
You do not need fetch anything to delete it. Just create Entity with specified Id, Attach it and then call Remove.