0

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1730326
  • 83
  • 2
  • 9

1 Answers1

0

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)

Community
  • 1
  • 1
Ulises
  • 13,229
  • 5
  • 34
  • 50
  • 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 01:19