C# ListView - control checkBox event

2019-09-03 15:15发布

Suppose I need to change the status of an item from active = true to active = false and vise versa and at the same time persist my change in the database table.

I tested ItemChecked event like the following:

    private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        ListViewItem item = (ListViewItem)sender;

        if (item != null)
        {
            Book b = (Book) item.Tag;

            b.MakeActive(item.Checked);
        }
    }

I failed.

Can anyone help me?

1条回答
闹够了就滚
2楼-- · 2019-09-03 16:14

in this case object sender is ListView and not ListViewItem your code should be this

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
   ListViewItem item = e.Item as ListViewItem;

    if (item != null)
    {
        Book b = (Book) item.Tag;

        b.MakeActive(item.Checked);
    }
}
查看更多
登录 后发表回答