HelpNeeder
HelpNeeder

Reputation: 6480

How to uncheck checked item in Viewlist?

I have tried this:

    foreach (ListViewItem item in lvPC.CheckedItems)
    {
        selectedTag = item.Tag.ToString();

        DialogResult result = MessageBox.Show
            ("Are you sure you want to remove this entry?",
            "Information", MessageBoxButtons.YesNo,
            MessageBoxIcon.Information);

        if (result == DialogResult.Yes)
        {
            // SQL query which will delete entry by using entry ID.
            string sql = "DELETE FROM PersonalData WHERE DataID = " + selectedTag;

            DeleteData(sql, selectedTag);

            DisplayFileContent(filePath);
        }
        else
        {
            if (lvPC.CheckedItems == CheckState.Checked)
                item = CheckState.Unchecked;
        }
    }

But clearly I don't know how to do this. How do I check state my my item? How to uncheck it?

Upvotes: 1

Views: 2600

Answers (1)

Jeff Ogata
Jeff Ogata

Reputation: 57783

You should need to worry about whether the item is checked since you are enumerating the CheckedItems, so you can just set the Checked property:

else
{
    item.Checked = false;
}

Also, just a side note, you might want to consolidate your message asking whether the user wants to delete the checked items; it will get very annoying to continually click 'Yes' if several items have been checked.

Better would be a single dialog simply asking "Are you sure you want to delete the selected items?" to catch an accidental button click. If the user isn't sure which items they've selected, they can always cancel the delete and make sure.

Upvotes: 2

Related Questions