Bookmark and Share

ASP.NET C#: Using LINQ with a List View, and the DataKeyNames property

Posted on Fri 6 March 2009

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.


Permalink | Comments (0)
Tags: , , , , , , . Categories: ASP.NET | C# | LINQ.

Comments

Comments

Add a comment

Name, e-mail and your comments are all required fields

biuquote
Loading



< go back to the main blog page

< go up to the top of the page