Visual Basic Net Datatable filter by column


I am being passed a data table.  The data table has 1 to n columns in it.  I know that will have three specific columns, i..e. firename, Surname and address.  I want to create a second dataset or limit the first data set to only include those columns.  NOTE:  I the dataset being passed to me I do not know how many or what the other columns are.  What is the best way to create this row filter?
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.

kintonAuthor Commented:
My last question was meant to read "What is the best way to create this column filter?"!
Jorge PaulinoIT Pro/DeveloperCommented:
What is you datatable (sql, access, etc)? Or is a system datatable ?
kintonAuthor Commented:
Its a datatable object is Visual Basic 2005
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

As you are on Net 2.0 you can use the DataView.ToTable method.  Have a look at this


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
Jorge PaulinoIT Pro/DeveloperCommented:
           Dim dt As New DataTable
            Dim index As Integer = 0
            ' Copy from other datatable
            dt = myDataSet.Tables(0)
            Dim total As Integer = dt.Columns.Count
            For x As Integer = 0 To dt.Columns.Count - 1
                If x >= (total + index) Then Exit For
                If dt.Columns(x - index).ColumnName <> "firename" And dt.Columns(x - index).ColumnName <> "Surname" And dt.Columns(x - index).ColumnName <> "address" Then
                    dt.Columns.Remove(dt.Columns(x - index))
                    total -= 1
                    index += 1
                End If

myobject.DataSource = dt.DefaultView

            dt = myDataSet.Tables(0)

won't COPY from the other datatable.  It IS the other datatable.  Just to demonstrate, try this.  One form, two datagridviews, one button, this code

Public Class Form1

    Private dt1 As New DataTable
    Private dt2 As New DataTable

    Private Sub filltables()
        Dim dc0 As New DataColumn("ID", GetType(Integer))
        Dim dc1 As New DataColumn("Item", GetType(String))
        dt2 = dt1
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DataGridView1.DataSource = dt1
        DataGridView2.DataSource = dt2
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
End Class

You'll notice that when you click on the button to remove a column from dt2, it goes from dt1 as well.

It may be that the old datatable can be altered in this way, but if so there's no need for a second one.  If there is to be a second one as a copy then it will be necessary to use DataTable.Copy.

But I still think

    dim dv as new dataview(oldtable)
    dim newtable as datatable = dv.ToTable("NewTable", False, New String() {"firename", "Surname", "Address"})

or some such would be neater ;-)

kintonAuthor Commented:
Perfect.  Thank you
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.