开发者

Is it possible to do a linq query on a GridView (ASP.NET)?

Basically I have a datakey that I'd like to qu开发者_StackOverflow社区ery 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.


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!


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.


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


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))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜