HOW TO DELETE MULTIPLE CHECKED ITEMS IN A LISTVIEW?

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#
 958  View(s)
Ratings:
Rate this:

protected void btndelete_Click(object sender, EventArgs e)

{

    DataTable dt = new DataTable();

    for (int i = 0; i < listview1.Items.Count; i++)

    {

        ListViewDataItem items = listview1.Items[i];

        CheckBox chkBox = (CheckBox)items.FindControl("chkdel");

        if (chkBox.Checked == true)

        {

            if (Session["CurrentTable"] != null)

            {

                dt = (DataTable)Session["CurrentTable"];

                dt.Rows.RemoveAt(i);

            }

        }

        else

        {

        }

    }        

    Session["CurrentTable"] = dt;

    listview1.DataSource = dt;

    listview1.DataBind();

    BindDataToGridviewDropdownlist();    

}

Here it is deleting one row only. How to delete multiple checked items in listview?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to delete multiple checked items in a ListView?

    Hi Ankit,

    you are taking data table from session each time but not actually updating it using Session["CurrentTable"]=dt; like:

    protected void btndelete_Click(object sender, EventArgs e)

    {

        DataTable dt = new DataTable();

        for (int i = 0; i < listview1.Items.Count; i++)

        {

            ListViewDataItem items = listview1.Items[i];

            CheckBox chkBox = (CheckBox)items.FindControl("chkdel");

            if (chkBox.Checked == true)

            {

                if (Session["CurrentTable"] != null)

                {

                    dt = (DataTable)Session["CurrentTable"];

                    dt.Rows.RemoveAt(i);

                    Session["CurrentTable"]=dt;

                }

            }

            else

            {

            }

        }

        Session["CurrentTable"] = dt;

        listview1.DataSource = dt;

        listview1.DataBind();

        BindDataToGridviewDropdownlist();    

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!