Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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?
Posted
Updated 24-Sep-13 0:33am
v2
Comments
VICK 24-Sep-13 6:23am    
rather than using the for loop ,, i think you should use foreach loop.. which will get the checked values and will do what you want.

Or you can use javascript for that..
VICK 24-Sep-13 6:28am    
What type of improvement you required???

Piece of code???
VICK 24-Sep-13 6:41am    
Have u tried placing debugger over the code and checked that either it is getting different item each time or ???

Hey there,

Try
C#
dt.AcceptChanges(); 
after
C#
dt.Rows.RemoveAt(i);

and move
C#
if (Session["CurrentTable"] != null)
{
dt = (DataTable)Session["CurrentTable"];
outside the loop
Also, in this statement,
C#
dt.Rows.RemoveAt(i);
you cannot use i as the index to delete. Introduce another variable e.g. int j = 0 and increment it only when the item is not deleted. This is because whenever you remove a row from DataTable, it will not longer match the data/indexes of listview and you'll get an IndexOutOfBoundsException exception.
Do something like this:
C#
protected void btndelete_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            if (Session["CurrentTable"] != null)
            {
                dt = (DataTable)Session["CurrentTable"];
                int j = 0;
                for (int i = 0; i < listview1.Items.Count; i++)
                {
                    ListViewDataItem items = listview1.Items[i];
                    CheckBox chkBox = (CheckBox)items.FindControl("chkdel");

                    if (chkBox.Checked == true)
                    {
                        dt.Rows.RemoveAt(j);
                        dt.AcceptChanges();
                    }
                    else
                    {
                        j++;
                    }
                }
                Session["CurrentTable"] = dt;
                listview1.DataSource = dt;
                listview1.DataBind();
                BindDataToGridviewDropdownlist();
            }
        }

best of luck
 
Share this answer
 
v3
Comments
tanweer 24-Sep-13 6:49am    
good answer:)
karthik mushyam 24-Sep-13 7:50am    
Thank you very much asif ...great ..it works ,...thank you very much
C#
protected void btndelete_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
var rowsToDelete = new List();
if (Session["CurrentTable"] != null)
{
dt = (DataTable)Session["CurrentTable"];
for (int i = 0; i < listview1.Items.Count; i++)
{
DataRow dr = dt.Rows[i];
ListViewDataItem items = listview1.Items[i];
CheckBox chkBox = (CheckBox)items.FindControl("chkdel");
if (chkBox.Checked == true)
{
rowsToDelete.Add(dr); 
} 
}
rowsToDelete.ForEach(x => dt.Rows.Remove(x));
Session["CurrentTable"] = dt;
listview1.DataSource = dt;
listview1.DataBind();
BindDataToGridviewDropdownlist();
}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900