0

I have a gridview with "select" row and paging. I´m getting an error when I try to change paging site.

Here's the image of the problem

I can get rid of this issue if i use If() statement but then my select event wont work.

/*************Acitivate "Search" column for every row in gridview******************/
    protected void gwActivity_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        /*if (e.CommandName.ToString() == "Select")*/

        {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);           
            txtActivity.Text = row.Cells[2].Text;
            ddlStatus.SelectedValue = row.Cells[4].Text;
            ddlResponsible.SelectedValue = row.Cells[5].Text;
            ddlCategory.SelectedValue = row.Cells[6].Text;
            ddlPriority.SelectedValue = row.Cells[7].Text;
            ddlSize.SelectedValue = row.Cells[8].Text;
            ddlSystem.SelectedValue = row.Cells[9].Text;
            ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
            txtComment.Text = row.Cells[11].Text;


        }

    }

ERROR:

An exception of type 'System.InvalidCastException' occurred in App_Web_rsb5hpia.dll but was not handled in user code Additional information: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
Nils
  • 444
  • 3
  • 8
  • 22

2 Answers2

0

Try it changing LinkButton to Control:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

Or use this:

GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;

Or this one:

GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);
5377037
  • 9,493
  • 12
  • 43
  • 73
  • Hi, the last one looks like it works not 100% sure because an error comes on row: txtActivity.Text = row.Cells[2].Text; ERROR:Additional information: Object reference not set to an instance of an object. – Nils Apr 03 '18 at 14:46
0
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(((Label)gvListadoActividades.Rows[e.RowIndex].FindControl("MyId")).Text);
        this.DeletedMyid(id);
        this.GridView1.EditIndex = -1;
        this.LoadMyData();
    }