将Gridview转换为RadGrid

时间:2012-11-29 01:14:41

标签: asp.net telerik telerik-grid radgrid

如何将以下代码从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);
    }
}

1 个答案:

答案 0 :(得分:2)

而不是

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

使用

GridDataItem row = e.Item as GridDataItem;

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

或者更好的是,如果合适,请尝试使用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");