Home > DeveloperSection > Forums > How to delete multiple checked items in a ListView?
Ankit Singh

Total Post:341

Points:2389
Posted on    September-24-2013 4:22 AM

 C#
Ratings:


 1 Reply(s)
 749  View(s)
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?



Pravesh Singh

Total Post:411

Points:2881
Posted on    September-24-2013 7:39 AM

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();    

}


Don't want to miss updates? Please click the below button!

Follow MindStick