This example shows how to iterate over items in a listview to find all those that have been selected using LINQ. I used it originally for an email inbox – you select any number of messages and then hit delete, this gets all the selected items, find the datakeynames (primary key) and deletes that item.

The key parts are:

Selecting the primary key for the row we are on, as we set the DataKeyNames property for the ListView:

Customers_LstVw.DataKeys[dataItem.DataItemIndex].Value

And the LINQ to find all the items that have been selected:

IEnumerable<ListViewDataItem> query = (from dataItem in Customers_LstVw.Items.Cast()
where ((CheckBox)dataItem.FindControl("Select_ChckBx")).Checked == true
select dataItem);

This project uses the Northwind database.

Click here to download the sample project and see all the code.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

2,918 Spam Comments Blocked so far by Spam Free Wordpress

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>