Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

When it came time to start catching and handling exceptions in my program, I had never thought to use the events for ItemInserted, ItemDeleted and ItemUpdated for the ListView. They are incredibly useful.

The events arguments passed as the second parameter expose some very useful properties that let you manage exceptions, access previously entered values and more.

I’ve created a ListView and gone ahead and created the three events attached to it. Below you can see that the Intellisense has brought up the fact that I can check for exceptions:

 

image

So what’s useful about this is that I can actually write up some methods that will:

  1. Check for an exception and/or an inner exception
  2. Tell the Program that i have Handled the exception by setting “ExceptionHandled = true”
  3. Pass some sort of message back to the user (either the exception’s message, or some custom one that I write up.)
  4. Optionally for the insert/update modes, I can indicate to keep the ListView in “insert” mode, so that it doesn’t wipe the slate clean and make the user re-enter in everything.

Here’s an example method:

 

protected void ListView1_ItemInserted(object sender, ListViewInsertedEventArgs e)
{
        if (e.Exception != null) // If an exception occurred
        {
            if (e.Exception.InnerException != null) // First check for an inner exception
            {
                StatusLabel.Text = e.Exception.InnerException.Message; // I will just simply be passing the message to a visible label to the user
            }
            else
            {
                StatusLabel.Text = e.Exception.Message; // If there is No inner exception, I'm choosing to pass the exception message.
            }
            e.ExceptionHandled = true;  // Tell the program the exception is handled (by me)
            e.KeepInInsertMode = true; // Keep it in insert mode
        }
}

You can even refactor everything inside, and call it from multiple ListViews so that it’s all uniform. You can also get more complicated by checking for the type of exception that was thrown, by using if (e.Exception Is myCustomException) if statements. For example:

if (e.Exception is NullReferenceException)
   {
     // a null reference exception has occurred and handle it in a special way.
   }

 

I like checking for exceptions in the Handled events, since it allows me to write up back-end validation methods that throw exceptions if bad data has made its way all to the database statements. (i.e. If bad data was sent all the way to the insert method, and my method checked for it, and throws an exception, the ListView will handle it and report back the message that I wrote up in that custom exception.)

Hope this is helpful!

Cheers.

Posted on Monday, July 27, 2009 4:43 PM | Back to top


Comments on this post: Tutorial: ListView ItemDeleted, ItemUpdated and ItemInserted Events

# re: Tutorial: ListView ItemDeleted, ItemUpdated and ItemInserted Events
Requesting Gravatar...
Thank you for the info.
Left by Jeffrey on Aug 25, 2011 12:19 PM

# re: Tutorial: ListView ItemDeleted, ItemUpdated and ItemInserted Events
Requesting Gravatar...
Good Concept for exception.
Left by Pankaj Gupta on Dec 05, 2015 4:55 AM

Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net