I have 2 gridview on a page, the first is filled from a database and the second filled with the rows that I select in a checkbox, in each row of the gridview I have a button linked with a method to eliminate that particular row
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Cantidad" DataField="Cantidad"/>
<asp:BoundField HeaderText="Codigo" DataField="IdProducto"/>
<asp:BoundField HeaderText="Marca" DataField="NomMarca"/>
<asp:BoundField HeaderText="Ciudad" DataField="NomCiudad"/>
<asp:BoundField HeaderText="Tamaño" DataField="Tamano"/>
<asp:BoundField HeaderText="Fragilidad" DataField="NomFragilidad"/>
<asp:BoundField HeaderText="Ubicacion" DataField="NomUbicacion"/>
<asp:BoundField HeaderText="Proveedor" DataField="NomProveedor"/>
<asp:BoundField HeaderText="Producto" DataField="NomProducto"/>
<asp:BoundField HeaderText="Descripcion" DataField="Descripcion"/>
<asp:BoundField HeaderText="Existencias" DataField="Existencias"/>
<asp:BoundField HeaderText="Precio de Venta" DataField="PrecioVenta"/>
<asp:BoundField HeaderText="Precio de Compra" DataField="PrecioCompra"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btn_eliminar" OnClick="btn_eliminar_Click" runat="server" Text="Eliminar" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When I need to delete a particular row from the gridview2, the method I did at the beginning behaves well, but when there are 1 or 2 rows left, it no longer deletes more rows.
I need to eliminate those rows only from the gridview, not from the database, so that the user once he is sure inserts the data in another table of the database. If you need me to show other parts of the code just ask, thank you in advance!
protected void btn_eliminar_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
int indexdeboton = gvr.RowIndex;
DataTable dt = (DataTable)ViewState["GetRecords"];
dt.Rows[indexdeboton].Delete();
GridView2.DataSource = dt;
GridView2.DataBind();
}