ARTICLE

Implementing Group By in a DataSet in VB.NET

Posted by Mahesh Chand Articles | ADO.NET in VB.NET April 06, 2007
We know a DataSet does not support GROUP BY and recently I needed GROUP BY in one of my applications. This attached class shows how to implement GROUP BY in a DataSet.
Download Files:
 
Reader Level:

Recently I needed GROUP BY feature applied to a DataSet in VB.NET. I found the following article Implement a DataSet GROUP BY Helper Class in Visual C# .NET on MSDN, which was very useful.

Nipun Tomar helped me converting C# code to VB.NET, which is attached in this article. This is what CreateGroupByTable method looks like. See the attached source code for more details.

Public Function CreateGroupByTable(ByVal TableName As String, ByVal SourceTable As DataTable, ByVal FieldList As String) As DataTable

  If FieldList Is Nothing Then
    Throw New ArgumentException("You must specify at least one field in the field list."
)
  Else
    Dim dt As DataTable = New
DataTable(TableName)
    ParseGroupByFieldList(FieldList)

    For Each Field As FieldInfo In GroupByFieldInfo
       Dim dc As
DataColumn = SourceTable.Columns(Field.FieldName)
       If Field.Aggregate Is Nothing
Then
         dt.Columns.Add(Field.FieldAlias, dc.DataType, dc.Expression)
       Else
         dt.Columns.Add(Field.FieldAlias, dc.DataType)
       End
If
    Next

  If Not (ds Is Nothing) Then
    
ds.Tables.Add(dt)
  End If

  Return dt
End
If
End Function

 

 

 

 

share this article :
post comment
 

Thank you for great class!
But why average (avg) operator is missed?

Posted by ONPR Jun 08, 2010

many thanks

Posted by tArun cHatterjee Apr 29, 2010

Hi,

I am developing one windows application in vb.net which creates digitally signed pdf. I am getting one exception (System.OutOfMemoryexception) after generating some (1200) pdf. I've properly closed all objects and flushed the file stream which i've used in the application. But still i am getting the error.

Any Idea?





Posted by kumar raja Aug 29, 2009

Hi Mahesh,

I got it from InsertGroupByInto. Its working fine.

Thank you.

Posted by kumar raja Aug 29, 2009

Dear Mahesh,

I'd tried to use your DatasetHelper. Called CreateGroupByTable by passing table name, Datatable and field which datatable to be group by but it returns me only datatable with column name which i'd passed for groupby and without data.

Please clarify ...

Thank you for providing such usefull article.


Posted by kumar raja Aug 29, 2009
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor