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
mmalik15Asked:
Who is Participating?
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.

REA_ANDREWCommented:
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
0
VBRocksCommented:
It's real simple to do a filter on a DataTable, here's an example:

        Dim table As New DataTable()
        table.Columns.Add("ID")
        table.Columns.Add("Department")

        'Add 10 rows
        For i As Int16 = 1 To 10
            table.Rows.Add(i, "Department " & i.ToString())
        Next

        'Set filter
        Dim view As DataView = table.DefaultView
        view.RowFilter = "Department LIKE '*5'"    'Filter for *5

        'Iterate through each item in the filtered view
        For Each row As DataRowView In view
            Debug.WriteLine(row.Item("Department"))
        Next

        Stop    'View Output Window


What are you thinking of when you say "distinct".  You can get distinct records, but just like typical
grouping, when you apply it to a DataTable, all of the data "distinct" rows must match.  If data in any
"matching" column doesn't match, then it won't qualify as distinct.  (If that makes sense).

VBRocks
0

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
VBRocksCommented:
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
0
mmalik15Author Commented:
excellent
0
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
.NET Programming

From novice to tech pro — start learning today.