ASP.NET C#: Using LINQ with a List View, and the DataKeyNames property
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.
-
Calendar
May 2012 M T W T F S S « Mar 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 -
Meta





