使用rowcommand进行分页

时间:2018-04-03 13:14:57

标签: c# gridview paging rowcommand

我有一个带有“select”行和分页的gridview。 我尝试更改分页站点时收到错误。

Here's the image of the problem

如果我使用If()语句,我可以摆脱这个问题但是我的选择事件不会起作用。

/*************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;


        }

    }

错误:

  

App_Web_rsb5hpia.dll中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理   附加信息:无法将类型为“System.Web.UI.WebControls.GridView”的对象强制转换为“System.Web.UI.WebControls.LinkBut​​ton”。

2 个答案:

答案 0 :(得分:0)

尝试将LinkButton更改为Control

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

或者使用它:

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

或者这个:

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

答案 1 :(得分: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();
    }