我有一个带有CRUD功能的gridview用户控件。我有一个OnRowDeleting事件和一个RowCommand事件来检查我的'删除'按钮是否被点击。具有删除行功能的GridView用户控件在删除后不会更新我的GridView
OnRowDeleting:
protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
grd.EditIndex = -1;
pnlSelected.Visible = false;
btnSave.Visible = false;
btnInsert.Visible = false;
grd.DataBind();
}
RowCommand:
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
LinkButton lnkBtn = (LinkButton)e.CommandSource; // the button
GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent; // the row
GridView myGrid = (GridView)sender; // the gridview
string item = (string)myGrid.DataKeys[myRow.RowIndex].Value.ToString();
DataController.DeleteByPrimKey(item);
}
}
在我的网络表单,填补了我的用户的数据源。现在的问题是我想删除一条记录。
ucShowHouseList.DataSource = myDataSource;
如果我在我的gridview中删除一条记录,gridview不显示更改。如果我刷新页面,记录被删除。
可能是什么问题?
编辑:
我想我的问题是,在用户控件的数据源没有得到后删除更新。所以在我的网页表单的Page_Load中我填了我的用户的数据源:
ucShowHouseList.DataSource = myDataSource;
在行删除在我的用户我现在做的事:
grd.DataSource = DataSource;
grd.DataBind();
数据源是在我的用户一个IEnumerable属性我在我的webform中填写page_load。
public IEnumerable DataSource;
这将返回项目的集合,当我删除行,因为我的网页是不是重新加载,因此收集不能被更新不更新。
如何解决?
注意:不是'lnkBtn.Parent.Parent'使用'lnkBtn.NamingContainer'来获取'GridViewRow'。根据你的问题,在你更改'DataSource'之后,你有'DataBind'' GridView'吗? –
没关系我通过重新填充我的数据源,然后重新绑定它来解决我的问题。我的问题现在解决了 –