views:

30

answers:

3

I have a DataTable that was built from dynamically created SQL, so I do not know the number of columns in the datatable.

How can I convert this datatable into an IList?

EDIT: I am then going to use this to send to a Telerik Grid on the page.

A: 

You can create an IList<Dictionary<string, object>> like this:

table.AsEnumerable()
     .Select(r => table.Columns.ToDictionary(c => c.ColumnName, c => r[c]))
     .ToList();
SLaks
A: 
var list = new List<DataRow>();

foreach (var row in table.Rows)
   list.Add(row);

return list;
Brian
I don't think that's what he wants.
SLaks
+1  A: 

To answer your edited question, you can simply bind the grid directly to the DataTable. (Or to its DefaultView)

You don't need a separate IList.

SLaks
But if I don't know the column names how can I display it using the MVC Grid?
John
@John: The grid should automatically create columns. Also, you can find the column names by looking at the `ColumnName` property in the table's `Columns` collection.
SLaks