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

Just a quick tip today, for something that I use often, and maybe others are wondering what’s the fastest way to figure out what item is actually calling the command.


So for our ListView_Command method, we have a “ListViewCommandEventArgs” parameter, which exposes the following properties;



So we can get to the Item, but that doesn’t really help us figure out what Item is actually calling the command. Easy enough. I’ll need to use e.Item to get to the index.

First I’ll make it into a ListViewDataItem, since in this case, I know it will be one (The command can’t come from an EmptyItem or InsertItem in my example.). You can always program in an If check to avoid casting incorrectly.

Here’s where I’m at now:




ListViewDataItem exposes some properties that a regular ListViewItem does not have. The ones of interest:


Great! Now we know the DataItemIndex, which we can use for whatever reason you might need. It’s typically useful for figuring out dataKeys for that item. For example, now I can do this:



I know exactly what the Item is, and I can gain really easy access to any DataKeys I might have set for that item, such as a primary key, a timestamp column, etc.


Hope this helps!



Posted on Friday, December 11, 2009 3:24 PM Quick Tips | Back to top

Comments on this post: QuickTip: Getting ItemIndex from ListView_Command event

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © samerpaul | Powered by: