views:

29

answers:

3

Hi,

I have a listview with a the property checkbox = true.

When the user clicks on the checkbox and changes its state (checked -> unchecked or unchecked -> checked), I catch the ItemCheck event and do some DB implementation.

I want to ask the user for confirmation before working with the DB.

When I the user cancel it's command, I want that the checkbox will return to it's status.

How can I tell the listview to ignore the state change of the checkbox?

Thank,

Mattan

A: 

Check out the OnClientClick attribute of the checkBox:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=93&AspxAutoDetectCookieSupport=1

Using this you can cancel the postback, as well as set the value of the checkbox back to what it was before. If you use a templatefield instead of the checkbox=true property, you can add the OnClientClick attribute to the checkbox there; otherwise you need to add it dynamically in the ListView ItemDataBound event.

EDIT Oops, didn't see the "winforms" tag; thought this was ASP.Net (which also has a ListView control). Kindly disregard.

GendoIkari
+2  A: 

In the ItemCheck event, set the NewValue to the CurrentValue:

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (MessageBox.Show(this, "Change?", "Test", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
        e.NewValue = e.CurrentValue;
}
adrift