e4rthdog
e4rthdog

Reputation: 5223

How to get the indexes of checked items in a ListView Control

I have a ListView control (SfListView) for which i need to know the index of each item that is checked.

enter image description here

On the picture above that would be 1 , 3 ,4

CheckedItems Collection doesn't have the index.

DataSource property has the index along with the Data but of course it does not contain the checked state.

The other thing i tried is to manage it on the ItemChecked event like this:

private void lstAppNamePTT_ItemChecked(object sender, Syncfusion.WinForms.ListView.Events.ItemCheckedEventArgs e)
        {
            if(e.NewState == CheckState.Checked) {
                AppChecked.Add(e.ItemIndex);
            } else
            {
                AppChecked.Remove(e.ItemIndex);
            }
        }

Bit that doesn't give me a way to do it outside the event.

I thought of iterating the DataSource and find the key field value and retrieve the index, but is that the proper thing to do?

Upvotes: 0

Views: 231

Answers (1)

e4rthdog
e4rthdog

Reputation: 5223

I am putting here the answer i got from the SyncFusion forums:

LINK

It can be achieved by checking the checked items with items in View of SfListVIew and getting their index value:

ObservableCollection<int> selectedIndex = new ObservableCollection<int>(); 

            foreach (var allItems in sfListView1.View.Items) 
            { 
                foreach (var checkedItem in sfListView1.CheckedItems) 
                { 
                    if(allItems == checkedItem) 
                    { 
                        selectedIndex.Add(sfListView1.View.DisplayItems.IndexOf(allItems)); 
                    } 
                } 
            } 

Upvotes: 2

Related Questions