grdview_RowDeleting DataItem 为空

发布于 2024-11-09 01:10:07 字数 484 浏览 0 评论 0原文

我已将数据网格绑定到列表。我遵循存储库模式并使用 EF v4.1。我需要删除 row_deleting 事件上的实体。这是代码:

    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.
    }

当我使用实体时,我需要实际的实体将其传递到我的 GenericRepository,这将删除该实体。我确实获得了 bookId,但我不想做一些愚蠢的事情,例如使用此 bookId 从数据库中获取实体,然后将其传递给我的删除方法。为什么 DataItem 始终为空,我该怎么做才能取回我的实体?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-11-16 01:10:07

DataItem = null 因为当您在 Page_Load 中将数据绑定到 gridview 一次时,

public void Page_Load()
{
    if (!Page.IsPostBack)
    {
        grid.DataSource = GetDataSource();
        grid.DataBind();
    }
}

数据将存储在 Viewstate 中。单击删除后,您将回发到服务器。数据仍然存在于 Gridview 的视图状态中并且不会反弹。

如果您有 Id,则可以删除该对象。您可以通过 3 种不同的方式来完成此操作

  1. http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retriving-it。 aspx

  2. 调用 context.ExecuteStoreCommand 并执行常规 DELETE FROM -http://msdn.microsoft.com/en-us/library/ee358769.aspx(第二个示例)

  3. 在数据库上创建存储过程,将其导入模型并使用上下文调用它。 ExecuteFunction()

DataItem = null because when You databind data to gridview only once in Page_Load

public void Page_Load()
{
    if (!Page.IsPostBack)
    {
        grid.DataSource = GetDataSource();
        grid.DataBind();
    }
}

data is stored in Viewstate. After You click delete You postback to server. Data is still present in Gridview's viewstate and not rebound.

You can delete the object if You have Id. You can do it in 3 different ways

  1. http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retrieving-it.aspx

  2. call context.ExecuteStoreCommand and just do the regular DELETE FROM -http://msdn.microsoft.com/en-us/library/ee358769.aspx (2nd example)

  3. create stored procedure on database, import it to model and the call it using context.ExecuteFunction()

清风挽心 2024-11-16 01:10:07

您不需要获取任何内容即可将其删除。只需创建具有指定 ID 的实体,附加它,然后调用删除。

Person user = new Person() { PersonId = Id };
db.Persons.Attach(person);
db.Persons.Remove(person);
db.SaveChanges();

You do not need fetch anything to delete it. Just create Entity with specified Id, Attach it and then call Remove.

Person user = new Person() { PersonId = Id };
db.Persons.Attach(person);
db.Persons.Remove(person);
db.SaveChanges();
我是有多爱你 2024-11-16 01:10:07

我不确定我是否正确理解了你的问题,但我认为你应该尝试这个

grdBooks.Rows[e.RowIndex].Cells[index].Text; 

I am not suer whether I have understood your question right or not but I think you should try this

grdBooks.Rows[e.RowIndex].Cells[index].Text; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文