1

我想从RowDeleting事件 中的GridView获取 Id ,我尝试这样:

        protected void gvHouse_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid hholder = (Guid)(e.Values[0]);

            using (var dataContext = new realtydbEntities())
            {
                var house = (from h in dataContext.Households.Include("aspnet_Users.aspnet_Membership")
                             where h.HouseholdID == hholder
                             select h).First();
                dataContext.DeleteObject(house);
                dataContext.SaveChanges();
            }
        }

但它不起作用。

匿名类型是这样的,我使用 List< this anonymousType > 绑定到GridView的数据源:

                         new
                         {
                             Id = u.HouseholdID,
                             BuNo = u.Building.BuildingName,
                             UnNo = u.UnitNo,
                             DepNo = u.DepartmentNo,
                             Name = u.aspnet_Users.UserName,
                             Corp = u.Corporation,
                             IdNumber = u.IdNumber,
                             Telephone = u.Telephone,
                             OtherHouseMembers = u.OtherHouseMembers,
                             OtherInfo = u.OtherInfo,
                             CrDate = u.aspnet_Users.aspnet_Membership.CreateDate
                         });

我想获取匿名对象或只是获取 ID 属性的值。

4

3 回答 3

2

如果您使用的是 .net 4,则可以利用以下dynamic类型:

// Get the reference to the bound item
dynamic item = gvHouse.Rows[e.RowIndex].DataItem; 
// Access item property
var itemId = item.Id;

请记住,您的类型的任何属性都不能通过智能感知获得,您只需要避免拼写错误。

于 2012-05-08T06:26:49.373 回答
1

这将为您提供 ID

 var HouseHoldid = Guid.Parse(gvHouse.Rows[e.RowIndex].Cells[0].Text);

如果 ID 是 int 类型,则:

 var HouseHoldid = Convert.ToInt32(gvHouse.Rows[e.RowIndex].Cells[0].Text);

删除后,您还必须刷新数据源。从绑定列表中删除项目并刷新gridview的数据源

于 2012-05-08T05:35:38.663 回答
0

这种方式可以获取数据源

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    DataTable sourceData = (DataTable)GridView1.DataSource;

    sourceData.Rows[e.RowIndex].Delete();
}
于 2012-06-28T10:10:23.613 回答