VB.net 2005 (.net 2.0) - Group By DataTable

I need to Filter / Group By a DataTable
I need to be able to do a Select Account Number = 1111 and then Group those results by the Product Code

How can I Filter a DataTable and Group the Results?

This is an example...but it doesn't work...
Dim foundRows() As DataRow = DE_ProductCodes.Select("AccountNum = " & AcctNum.Text & " group by ProductCode")

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
You can use the DataView class to select your rows then apply a little logic to get the grouping to work.

Public Function GroupBy(i_sGroupByColumn As String, i_sAggregateColumn As String, i_dSourceTable As DataTable) As DataTable

	Dim dv As New DataView(i_dSourceTable)

	'getting distinct values for group column
	Dim dtGroup As DataTable = dv.ToTable(True, New String() {i_sGroupByColumn})

	'adding column for the row count
	dtGroup.Columns.Add("Count", GetType(Integer))

	'looping thru distinct values for the group, counting
	For Each dr As DataRow In dtGroup.Rows
		dr("Count") = i_dSourceTable.Compute((Convert.ToString("Count(") & i_sAggregateColumn) + ")", (i_sGroupByColumn & Convert.ToString(" = '")) + dr(i_sGroupByColumn) + "'")

	'returning grouped/counted result
	Return dtGroup
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smithmrkAuthor Commented:
Sorry I didn't back to you sooner!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.