0

如何将以下代码从 GridView 更改为 RadGrid?下面是我的gridview的代码:

protected void gv_Movie_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //get the row number of the selected row
    int rowNo = int.Parse(e.CommandArgument.ToString());
    //get the selected row
    GridViewRow row = gv_Movie.Rows[rowNo];

    //Get movie ID, which is on the 1st column of the gridview

    string movieID = row.Cells[0].Text;
    if (e.CommandName == "Select")
    {
        Response.Redirect("MovieSelect.aspx?id=" + movieID);
    }
    else if (e.CommandName == "Update")
    {
        Response.Redirect("MovieUpdate.aspx?id=" + movieID);
    }
}

我尝试了以下代码,但由于e.CommandArgument. 有什么解决办法吗?

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    int rowNo = int.Parse(e.CommandArgument.ToString());

    GridDataItem row = RadGrid1.Items[rowNo];

    string movieID = row.Cells[0].Text;
    if (e.CommandName == "Select")
    {
        Response.Redirect("movieSelect.aspx?id=" + movieID);
    }
    else if (e.CommandName == "Delete")
    {
        Response.Redirect("movieUpdate.aspx?id=" + movieID);
    }
}
4

1 回答 1

2

代替

 int rowNo = int.Parse(e.CommandArgument.ToString());
 GridDataItem row = RadGrid1.Items[rowNo];

采用

GridDataItem row = e.Item as GridDataItem;

Telerik 自动将当前行检索为 e.Item。其余的应该是一样的。

或者更好的是,如果合适的话,尝试使用 Utilize DataKeyNames

因此,在您的标记中,您将拥有如下内容:

<telerik:RadGrid id="grid" runat="server">
   <MasterTableView DataKeyNames="movieID">
      .....
   </MasterTableView>
</telerik:RadGrid>

然后,您可以像这样检索movieID:

 var row = e.Item as GridDataItem;  
 string movieID = row.GetDataKeyValue("movieID");
于 2012-11-29T02:14:26.637 回答