Solving index out of range error
<p>I am using gridview row commands edit and delete. When I click on those links that particular row data shows on textboxes as mentioned.</p>

<p>First loop is working but for <code>e.CommandName == "deletevalue"</code> showing an error </p>

<blockquote>
  <p>index was out of range. must be non-negative and less than the size of the collection</p>
</blockquote>

<pre><code>protected void GrdBuilding_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        int index = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "editvalue")
        {

            TxtBuildingid.Text = GrdBuilding.Rows[index].Cells[1].Text;
            TxtBuildingCode.Text = GrdBuilding.Rows[index].Cells[2].Text;
            TxtBuildingName.Text = GrdBuilding.Rows[index].Cells[3].Text;
            DrpBuilding.Text = GrdBuilding.Rows[index].Cells[4].Text;
            if (GrdBuilding.Rows[index].Cells[5].Text.ToString() == "True")
            {
                ChkisActive.Checked = true;
            }
            else
            {
                ChkisActive.Checked = false;
            }

            BtnSave.Text = "Update";
        }



        else if (e.CommandName == "deletevalue")
        {
            TxtBuildingid.Text = GrdBuilding.Rows[index].Cells[1].Text;
            TxtBuildingCode.Text = GrdBuilding.Rows[index].Cells[2].Text;
            TxtBuildingName.Text = GrdBuilding.Rows[index].Cells[3].Text;
            DrpBuilding.Text = GrdBuilding.Rows[index].Cells[4].Text;
            if (GrdBuilding.Rows[index].Cells[5].ToString() == "True")
            {
                ChkisActive.Checked = true;
            }
            else
                ChkisActive.Checked = false;
            }

            BtnSave.Text = "Delete";
        }
</code></pre>
