Remove a row from my ASP.NET gridview

0

How can I remove a row from my gridview in asp.net once I select a record, without affecting the database, ie the list is in memory.

This is the code as I charge my GriewView:

    private void Cargar()
    {
        srvLeyenda = new Srv_Leyenda();
        GridView1.DataSource = null;
        listadoLeyendas= srvLeyenda.LeerLeyendas();

        GridView1.DataSource = listadoLeyendas;
        GridView1.DataBind();
    }
    
asked by magi0 10.09.2018 в 06:15
source

1 answer

4

The elements of the grid should be removed from the data source, in your case the list you use and you should keep in memory:

private void Cargar()
{
    var srvLeyenda = new Srv_Leyenda();
    var listadoLeyendas = srvLeyenda.LeerLeyendas();

    Session["leyendas"] = listadoLeyendas;

    GridView1.DataSource = listadoLeyendas;
    GridView1.DataBind();
}


private void Remover(int id)
{
    var listadoLeyendas = Session["leyendas"] as List<Leyenda>

    listadoLeyendas = listadoLeyendas.Where(x=>x.id != id).ToList();

    Session["leyendas"] = listadoLeyendas; //actualizas los datos luego de remover

    GridView1.DataSource = listadoLeyendas;
    GridView1.DataBind();

}

As you will see, when you load the grid you keep the data in Session for later when you go eliminating the operation you perform it on this list without going to the database.

    
answered by 10.09.2018 в 15:28