0

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?

3
  • 1
    ListViewItem item = (ListViewItem)sender; will throw an exception if its an invalid cast so you will need to change that line to ListViewItem item = sender as ListViewItem; if you want an invalid cast to return null Commented Jun 10, 2009 at 16:07
  • Please describe how you fail. Do you get an exception - what do you expect to happen, what happens? Commented Jun 10, 2009 at 16:17
  • private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e) { ListViewItem item = (ListViewItem)e.Item; if (item != null) { if (item.Tag != null) { Teacher t = ((Teacher)item.Tag); t.TeacherGoesToLeave(item.Checked); } } } I became successful this way. Commented Jun 10, 2009 at 16:29

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.