mmalik15
asked on
how can we filter records on a datatable in vb.net
I have a readonly stored procedure that i use to pull data from a database and i store results in a datatable. Now i need to filter some information from this datatable. e.g. i need distinct departments from this datatable object which holds roughly 20 fields of information. I am using vb.net in VS 2005, Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is an example of how to get distinct records from a DataTable:
Dim table As New DataTable()
table.Columns.Add("ID")
table.Columns.Add("Departm ent")
'Add 10 rows to a table
For i As Int16 = 1 To 10
table.Rows.Add(i, "Department")
Next
'Get Distinct Rows:
' Enter any additional columns you want after "Department",
' separated by a comma
Dim distinctRecords As DataTable = _
table.DefaultView.ToTable( True, New String() {"Department"})
'Only 1 row will output:
For Each row As DataRow In distinctRecords.Rows
Debug.WriteLine(row.Item(" Department "))
Next
Stop 'View Output Window
VBRocks
Dim table As New DataTable()
table.Columns.Add("ID")
table.Columns.Add("Departm
'Add 10 rows to a table
For i As Int16 = 1 To 10
table.Rows.Add(i, "Department")
Next
'Get Distinct Rows:
' Enter any additional columns you want after "Department",
' separated by a comma
Dim distinctRecords As DataTable = _
table.DefaultView.ToTable(
'Only 1 row will output:
For Each row As DataRow In distinctRecords.Rows
Debug.WriteLine(row.Item("
Next
Stop 'View Output Window
VBRocks
ASKER
excellent
http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176
To filter normally on a datatable you would create a dataview and use RowFilter. There is lots you can do with ADO.NET, it is very powerful. The support article is a custom helper class microsoft show you how to make.
Andrew