开发者

LINQ Operation GridView

开发者 https://www.devze.com 2023-01-07 05:15 出处:网络
I have a GridView which populates IDNameCity 1BobbyAAA 2LauraBBB 3IlishaCCC I want to get an enumerable collection something like following

I have a GridView which populates

ID  Name    City
1   Bobby   AAA
2   Laura   BBB
3   Ilisha  CCC

I want to get an enumerable collection something like following

开发者_运维问答var  Query= from p in GridView1.Rows 
   select new { User_ID=p.ID,User_Name=p.Name,User_City=p.City }.ToList();

How to get it?

Thanks in advance.


To get an enumerable collection, all you need to do is cast the GridViewRowCollection to an IEnumerable.

 IEnumerable<GridViewRow> rows = GridView1.Rows.Cast<GridViewRow>()


If you're trying to use the actual object that the grid row represents, then you need to access the DataItem property of the row.

var query = from p in GridView1.Rows.Select(r => r.DataItem as YourDataType)
            select new 
            { 
                User_ID=p.ID,
                User_Name=p.Name,
                User_City=p.City 
            }.ToList()

(YourDatatype here represents whatever type of object you're expecting the grid to be bound to).


You are on the right Track with the query.

The only suggestion would be to try and use the original source which binds to the gridview instead of relying on gridview.DataSource

0

精彩评论

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