views:

61

answers:

4

Basically I have a datakey that I'd like to query from my GridView instead of looping through all the rows and comparing the key of each row. So I was wondering if it was possible to just do a linq query on the gridview (not datatable) and filter with the datakey.

+1  A: 

Gridview in itself is nothing. It's just a UI, the data is in its source be it a datatable or dataset and you can use linq to query them.

Sidharth Panwar
+1: I'd go so far as to say "Always work on the data, never its presentation."
SnOrfus
+1  A: 

As far as I understand the theory of LINQ, it can be performed on any list. As a datasource in a gridview is in essence a list, you should be able to use LINQ on that datasource of the gridview.

Try this example:

http://weblogs.asp.net/scottgu/archive/2006/05/14/Using-LINQ-with-ASP.NET-_2800_Part-1_2900_.aspx

Michael Eakins
A: 

I did something similar to this with a repeater, and maybe it will help... or you can just disregard it if it doesn't. In my situation I databound the repeater and allowed users to modify the data before exporting it to XML. The following LINQ loops through each row in the repeater, uses findcontrol to grab the control from the datarow and then uses Linq to XML, but this could be used to generate objects using LINQ to Objects. rp is the repeater, cbIgnore is a checkbox which if the users checks it the row is not expored.

    Dim doc As New XDocument( _
        New XDeclaration("1.0", "ISO-8859-1", "true"), _
        New XElement("Schedule_Import", _
                     From c As RepeaterItem In rp.Items _
                     Where (c.ItemType = ListItemType.Item Or c.ItemType = ListItemType.AlternatingItem) _
                     AndAlso DirectCast(c.FindControl("cbIgnore"), HtmlInputCheckBox).Checked = False _
                     Select New XElement("activity", _
                                         New XElement("code", DirectCast(c.FindControl("txtAC"), TextBox).Text), _
                                         New XElement("starttime", DirectCast(c.FindControl("dtfStart"), DateTimeField).SelectedDateTime), _
                                         New XElement("endtime", DirectCast(c.FindControl("dtfEnd"), DateTimeField).SelectedDateTime), _
                                         New XElement("description", DirectCast(c.FindControl("txtTitle"), TextBox).Text))))
Patricker
A: 

Not sure how to use DataKeyNames directly, because Column doesn't have any information about data field name it's coming from. In the example below, I use SortExpression to get column index which is used for filtering.

EDIT: The most important part here is the casting, which enables you to use all the fancy extension methods designed for IEnumerable<T>.

int idColumnIndex = MyGrid.Columns.Cast<DataControlField>().Where(e => e.SortExpression == "ID").Select(e => MyGrid.Columns.IndexOf(e)).FirstOrDefault();
var row = MyGrid.Rows.Cast<GridViewRow>().Where(e => e.Cells[idColumnIndex].Text == "421").FirstOrDefault();

Everything is possible!

Radex