Link to home
Start Free TrialLog in
Avatar of mmalik15
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
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

if you want to achieve a distinct rowfilter in ado.net then please look at this support article.

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
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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("Department")

        '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
Avatar of mmalik15
mmalik15

ASKER

excellent