Error retrieving cell value in GridView

2

I am carrying out a program so that certain students can choose their project, however, in order for the student to correct or change a member of his team, he must delete his own or his partner's registration before registering in the database. data. Everything works correctly, however, I get the error "System.ArgumentOutOfRangeException" when I retrieve the cell with the Matricula

The Grid is next

<asp:GridView ID="gv_Equipo" runat="server" CssClass="table table-hover" ShowHeaderWhenEmpty="True" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            asp:BoundField DataField="Matricula" HeaderText="Matricula" />
            <asp:BoundField DataField="Nombre" HeaderText="Nombre" />
            <asp:BoundField DataField="ApellidoPaterno" HeaderText="Apellido paterno" />
            <asp:BoundField DataField="ApellidoMaterno" HeaderText="Apellido materno" />
            <asp:BoundField DataField="Grado" HeaderText="Grado" />
            <asp:BoundField DataField="Grupo" HeaderText="Grupo" />
            <asp:TemplateField>
            <HeaderTemplate>Opciones</HeaderTemplate>
               <ItemTemplate>
                <asp:Button runat="server" ID="btnEliminar" Text="Eliminar" CommandName="Select" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CssClass="btn btn-outline-danger" OnCommand="btnEliminar_Command" OnClientClick="return confirmOrderDel(this);" />
                </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

and the code to recover is as follows.

        protected void btnEliminar_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            dtIntregrantes = (DataTable)Session["Integrantes"];

            int index = Convert.ToInt32(e.CommandArgument.ToString());

            gv_Equipo.DataSource = dtIntregrantes;

            alBO.Matricula = Convert.ToInt32(gv_Equipo.Rows[index].Cells[0].Text);

            DataRow foundRow = dtIntregrantes.Rows.Find(alBO.Matricula);

            if(foundRow != null)
            {
                dtIntregrantes.Rows.Remove(foundRow);
            }
            dtIntregrantes.AcceptChanges();
            Session["Integrantes"] = dtIntregrantes;
        }
        gv_Equipo.DataSource = dtIntregrantes;
        gv_Equipo.DataBind();
    }

    
asked by GreedSource 10.09.2018 в 08:18
source

1 answer

2

I managed to solve it by bindear the gridview, when filling it with a DataTable, in addition to filling it when adding new members of the team, I have to fill it before recovering the data.

protected void btnEliminar_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            dtIntregrantes = (DataTable)Session["Integrantes"];

            int index = Convert.ToInt32(e.CommandArgument.ToString());

            //esta es la solución
            gv_Equipo.DataSource = dtIntregrantes;
            gv_Equipo.DataBind();

            alBO.Matricula = Convert.ToInt32(gv_Equipo.Rows[index].Cells[0].Text);

            DataRow foundRow = dtIntregrantes.Rows.Find(alBO.Matricula);

            if(foundRow != null)
            {
                dtIntregrantes.Rows.Remove(foundRow);
            }
            dtIntregrantes.AcceptChanges();
            Session["Integrantes"] = dtIntregrantes;
        }
        gv_Equipo.DataSource = dtIntregrantes;
        gv_Equipo.DataBind();
    }
    
answered by 10.09.2018 / 08:26
source