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

Basically, I have a datakey that I would like to query from my GridView, rather than looping all the rows and comparing the key of each row. So I was wondering if it is possible to simply execute the linq query in gridview (not amenable to data processing) and filter using datakey.

+3
source share
4 answers

You do not know how to use DataKeyNames directly, because the column does not contain information about the name of the data field from which it is obtained. In the example below, I use SortExpression to get the index of the column that is used for filtering.

EDIT: - , , 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();

!

+3

Gridview . , - , linq .

+1

- , , , ... , . XML. LINQ , findcontrol datarow, Linq XML, LINQ to Objects. rp , cbIgnore - , , , .

    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

Source: https://habr.com/ru/post/1764507/


All Articles