本文介绍了如何使用Entity Framework 4.0在gridview中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用实体框架4.0在gridview中删除行.该行没有删除,这是我的代码:

How to delete a row in gridview using entity framework 4.0. The row is not deleting and this is my code:

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!

推荐答案

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



现在,当您说网格视图时,您是在谈论列表视图的网格视图模式吗?如果是这样,则应将ItemsSource绑定到您的实体的可观察集合.然后,您可以从可观察的集合中删除实体,然后网格将自动更新.



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.


这篇关于如何使用Entity Framework 4.0在gridview中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:15