从对象类型System.Web.UI.WebControls.GridViewRow到已知的托管提供程序本机类型不存在映射

时间:2014-01-21 00:35:18

标签: c# asp.net sql gridview

我想制作一个gridview,当我选择一行时,它的值将填充到另一个gridview和文本框,但我遇到上面的错误。当我单击GridView2中的行时,没有任何事情发生,并且sqladapter中出现错误。请帮我修改一下这段代码..

这是我的代码: C#

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conn);
    SqlCommand com = new SqlCommand("SELECT MRPNo, MRPType, MRPDate FROM MRP WHERE MRPNo = @mrpno",con);
    com.Parameters.Add("@mrpno", GridView2.SelectedRow);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(com);
    sda.Fill(dt);
    DataRow row = dt.Rows[0];
    txtMRPNo.Text = row["MRPNo"].ToString();
    txtMRPDate.Text = row["MRPType"].ToString();
    txtMRPDate.Text = row["MRPDate"].ToString();
    GridView3.DataBind();
    GridView1.DataBind();
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    LinkButton selectButton = new LinkButton()
    {
        CommandName = "Select",
        Text = e.Row.Cells[0].Text,
    };
    e.Row.Cells[0].Controls.Add(selectButton);
}

protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.RowIndex);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您不能使用GridView2.SelectedRow作为Parameters.Add()的输入。请参阅MSDN(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.110).aspx),您将看到原因。