2012-04-25 86 views
1

我的GridView行命令的代码看起来像网格视图Row_Command问题

protected void GridView_Admins_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     GridView sourceGrid = sender as GridView; 

     int currentIndex = 0; 
     GridViewRow currentRow = null; 

     if (string.Compare(e.CommandName, "SHOW", true) == 0) 
     { 
      if (int.TryParse(e.CommandArgument.ToString(), out currentIndex)) 
      { 

       currentRow = sourceGrid.Rows[currentIndex]; 

       if (long.TryParse(sourceGrid.DataKeys[currentRow.RowIndex].Values["EmployeeID"].ToString(), out this.employeeId)) 
        this.ShowAdministrativeRightsForUser(); 
      } 
     } 
    } 

我也已经在GridView中启用分页。当我想编辑记录时,我点击特定的单元格,然后我可以编辑记录。然而,当我在第二页上,当我点击一个单元格来编辑记录时,我在第currentRow = sourceGrid.Rows[currentIndex];行发生错误,说索引超出范围。 什么可能是错的?

回答

0

使用此获得指数:

int index = Convert.ToInt32(e.CommandArgument); 
+0

不,这并不work..I我仍然得到同样的错误“索引超出范围必须大于集合的大小非负少。 。 参数名称:索引“ – user1181942 2012-04-25 11:56:08

+0

你是如何设置CommandArgument的?你可以添加你正在使用的原始问题的代码吗? – Max 2012-04-25 14:20:45