gridview的错误

时间:2016-09-15 14:12:43

标签: c# .net grid

我有一个gridview ASP.net,可以从MS-SQL数据库中检索某些列。它工作正常,正确显示数据,我可以根据需要使用gridview附带的按钮进行编辑和删除。

我现在在数据库中添加了一个新列,并且正在更新我的代码。

我创建了一个新的datasource,我现在能够从数据库中检索该列。我在gridview中看不到editdelete时遇到问题,但我能够设法使用AutoGenerateDeleteButton="true"AutoGenerateEditButton="true"来显示它们。

但是,当我按下删除或编辑按钮时,我收到以下错误。

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. `

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

Source Error: 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnkbtn = ((LinkButton)e.CommandSource);
GridViewRow gvRow = (GridViewRow)(lnkbtn.NamingContainer);
HiddenField hdn_supplier = (HiddenField)gvRow.FindControl("hdn_supplier");
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是我目前正在进行的VB代码的c#示例。看起来你错过了你的删除推荐处理程序

protected void dgGlossaries_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   dynamic rowToDelete = e.Item.ItemIndex;
   DataTable dt = (DataTable)Session["glossaries"];
   DataRow dr = dt.Rows(rowToDelete);
   dr.Delete();
   dgGlossaries.DataSource = dt;
}

这是VB中的相同示例,如果您遇到c#示例,那么希望能指出正确的方向

Protected Sub dgGlossaries_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgGlossaries.DeleteCommand
    Dim rowToDelete = e.Item.ItemIndex

    Dim dt As DataTable = DirectCast(Session("glossaries"), DataTable)
    Dim dr As DataRow = dt.Rows(rowToDelete)
    dr.Delete()

    dgGlossaries.DataSource = dt
    dgGlossaries.DataBind()
End Sub