views:

36

answers:

3

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.

A: 

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

Adam Robinson
A: 

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

kanchirk
A: 

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

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