Skip to content
Loading
i want to delete row in gridview from session    
  •                 
  •             
  •         
  •     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using Jewelleryonlineshop.Models;  
    8. using System.Data.SqlClient;  
    9. using System.Data;  
    10. namespace Jewelleryonlineshop {  
    11.  public partial class cart_view: System.Web.UI.Page {  
    12.   static List < Products > cart_list = new List < Products > ();  
    13.   protected void Page_Load(object sender, EventArgs e) {  
    14.    if (!IsPostBack) {  
    15.     load_data();  
    16.    }  
    17.   }  
    18.   void load_data() {  
    19.    if (Session["cart"] != null) {  
    20.     List < Products > prods = Session["cart"as List < Products > ;  
    21.     // DataTable dt = (DataTable)Session["cart"];  
    22.     products_gw.DataSource = prods;  
    23.     products_gw.DataBind();  
    24.    }  
    25.   }  
    26.   protected void products_gw_SelectedIndexChanged(object sender, EventArgs e) {}  
    27.   protected void products_gw_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
    28.    //List prods = Session["cart"] as List;  
    29.    //Session.Contents.Remove("cart");  
    30.    //Session["cart"] = prods;  
    31.    DataTable dt = new DataTable();  
    32.    //Session.Contents.Remove("cart");  
    33.    int index = products_gw.SelectedIndex;  
    34.    dt = (DataTable) Session["cart"as DataTable;  
    35.    //DataRow dr = dt.Rows[e.RowIndex];  
    36.    dt.Rows[index].Delete();  
    37.    dt.AcceptChanges();  
    38.    ////products_gw.EditIndex = -1;  
    39.    ////load_data();  
    40.    products_gw.DataSource = dt;  
    41.    products_gw.DataBind();  
    42.   }  
    43.   protected void products_gw_RowDataBound(object sender, GridViewRowEventArgs e) {  
    44.    //if (e.Row.RowType == DataControlRowType.DataRow)  
    45.    //{  
    46.    // string id = (e.Row.FindControl("lblProductID") as Label).Text;  
    47.    // foreach (LinkButton button in e.Row.Cells[2].Controls.OfType())  
    48.    // {  
    49.    // if (button.CommandName == "Del")  
    50.    // {  
    51.    // button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + id + "?')){ return false; };";  
    52.    // }  
    53.    // }  
    54.    //LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");  
    55.    //if ((lnkDelete != null))  
    56.    //{  
    57.    // lnkDelete.Text = "Delete";  
    58.    // lnkDelete.CommandName = "Del";  
    59.    // lnkDelete.CommandArgument = e.Row.RowIndex.ToString();  
    60.    // lnkDelete.Attributes.Add("OnClick", "return confirm('Delete this row?');");  
    61.    //}  
    62.   }  
    63.   protected void products_gw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
    64.    products_gw.EditIndex = -1;  
    65.    load_data();  
    66.   }  
    67.   protected void products_gw_RowCommand(object sender, GridViewCommandEventArgs e) {  
    68.    //if (e.CommandName == "Del")  
    69.    //{  
    70.    // int RowsID = Convert.ToInt32(e.CommandArgument);  
    71.    // //List dt = null;  
    72.    // //Session.Contents.Remove("cart");  
    73.    // //dt = Session["cart"] as List;  
    74.    // Response.Redirect("cart_view.aspx");  
    75.    //}  
    76.   }  
    77.  }  
    78. }

    3 Replies

    Know the answer? Post it — somebody with the same question will find it here.