如何删除ListView中的多个已检查项? [英] How to delete multiple checked items in a ListView?

查看:54
本文介绍了如何删除ListView中的多个已检查项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里只删除一行。如何删除listview中的多个已检查项?


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

推荐答案

嘿那里,



尝试
Hey there,

Try
dt.AcceptChanges(); 



after

dt.Rows.RemoveAt(i);



和移动


and move

if (Session["CurrentTable"] != null)
{
dt = (DataTable)Session["CurrentTable"];

循环外

此外,在此声明中,

outside the loop
Also, in this statement,

dt.Rows.RemoveAt(i);

您不能使用i作为删除索引。引入另一个变量,例如int j = 0并仅在未删除项目时递增。这是因为无论何时从DataTable中删除一行,它都不会与listview的数据/索引匹配,您将获得IndexOutOfBoundsException异常。

执行以下操作:

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:

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


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


这篇关于如何删除ListView中的多个已检查项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆