Tuesday, September 21, 2010

Set Item selected inside listview.

I had strange issue while setting focus to list view Item. Let me share my exp with you.

I was trying to set focus on specific item of list view once list view is bound with the data. I tried with following code:

lstDetails.Items[i].Selected = true;
lstDetails.Items[i].Focused = true; 

It showed item as selected when I was in debug mode. Once I remove break point nothing was selected.

You need to write following code to item focused.
lstDetails.Items[i].Selected = true;
lstDetails.Items[i].Focused = true;
lstDetails.Items[i].EnsureVisible(); //This will set scroll position inside listview to the focused item.
lstDetails.Focus();     //This will set focus to the parent control that is listview.

Hope it will help you.

Thanks,
Ashish Chotalia

No comments:

Post a Comment