获取Gridview行索引

时间:2012-12-21 13:21:38

标签: c# asp.net gridview

我使用以下代码获取行索引

protected void gvESAPending_RowCommand(object sender, GridViewCommandEventArgs e)

    {
        try
        {
            lblMsg.Text = "";
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvESAPending.Rows[index]; // Here incorrect format error is coming
        }
     }

但索引值为0.这里有什么问题?

Aspx代码

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               “>                                                                                                                                                                                                                             “>                                                                                                                                                            

2 个答案:

答案 0 :(得分:1)

您可以添加OnRowCreteEvent

ASPX:

<asp:gridview id="gvESAPending" onrowcreated="gvESAPending_RowCreated" ...

CS:

protected void gvESAPending_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }

答案 1 :(得分:0)

GridViewRow row = gvESAPending.Rows[index];

通过执行此操作,您实际访问索引处的行。因此,如果index = 2,则实际返回gridviewrow中的第三行。