簡體   English   中英

如何刪除ListView中的多個選中項?

[英]How to delete multiple checked items in a 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();     
}

在這里,它僅刪除一行。 如何刪除列表視圖中的多個選中項?

正如上面的Hans Derks在評論中提到的那樣,您每次都從會話中獲取數據表,但實際上並沒有使用Session["CurrentTable"]=dt;對其進行更新Session["CurrentTable"]=dt; 喜歡:

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;
                listview1.DataSource = dt;
                listview1.DataBind();
            }
        }
        else
        {
        }
    }

    BindDataToGridviewDropdownlist();     
}

重構版的Pranav的答案:

protected void btndelete_Click(object sender, EventArgs e)
{
    // Get the Table from the session.
    DataTable dt = (DataTable)Session["CurrentTable"];
    // Only actually proceed, if we have a value.
    if(dt != null)
    {
        // Loop through each item.
        for (int i = 0; i < listview1.Items.Count - 1; i++)
        {
            // Find the checkbox to determine if it's checked.
            ListViewDataItem items = listview1.Items[i];
            CheckBox chkBox = (CheckBox)items.FindControl("chkdel");
            if (chkBox.Checked == true)
            {
                // Remove the row at the current index.
                dt.rows.RemoveAt(i);
            }
        }
        // Update the session and rebind the data.
        Session["CurrentTable"] = dt;
        listview1.DataSource = dt;
        listview1.DataBind();
        BindDataToGridviewDropdownlist(); 
    }
}

這對我有用...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM