views:

103

answers:

1

This below codes give me error below: How to generate this codes help me please!!!

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index



  protected void gvDepartman_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string selectedid = (gvDepartman).DataKeys[e.Row.RowIndex].Value.ToString();
                e.Row.Attributes["onclick"] = "location.href='Test.aspx?id=" + selectedid + "'";
            }
        }

A: 
  protected void gvDepartman_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex >=0)
        {
            string selectedid = (gvDepartman).DataKeys[e.Row.RowIndex].Value.ToString();
            e.Row.Attributes["onclick"] = string.Format("location.href='Test.aspx?id={0}'", selectedid);
        }
    }
ozczecho