Errors with gridview

本文关键字:gridview with Errors | 更新日期: 2023-09-27 18:07:04

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

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

我创建了一个新的datasource,现在我能够从数据库中检索该列。我在网格视图中没有看到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");
}

任何想法?

Errors with gridview

这是一个c#的例子,从工作的VB代码,我正在进行的时刻。看起来您丢失了删除推荐处理程序

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