Take the 2-minute tour ×

For a special requirements, we have created our own HTML table it works like a grid view. It gets its data from a DataTable and populates the TD and TR cells

We want to implement the Grouping functionality just like the grid view, where we specify the column name and all the values are grouped.

How can I perform the grouping directly in the DataTable and then output the rows as it is ? Can I use Linq ? Please help by directing towards an example.

share|improve this question
See stackoverflow.com/questions/1750054/… –  d.moncada Jan 14 '13 at 23:48

1 Answer 1

up vote 0 down vote accepted

You could try converting your DataTable to IEnumerable using AsEnumerable (make sure you add a reference to System.Data.DataSetExtensions).

Then use GroupBy to group your data. It will return IEnumerable<IGrouping<TKey, TSource>> where each IGrouping<TKey, TElement> object contains a collection of objects and a key. Example:

var groups = dt.AsEnumerable().GroupBy( r => r.Field<string>("someField") );

Update: You may group by using multiple fields (see example) or by nesting them (see example)

share|improve this answer
Thanks for your answer, It was helpful. Can I use the same GroupBy to perform multi column grouping. ie. first Group all the values from Column1, then Group all the values from Column2 ? –  user1730326 Jan 15 '13 at 1:19
Yes you can, see updated answer. –  Ulises Jan 15 '13 at 2:59

Your Answer


