Visual Basic Net Datatable filter by column

Hi,

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?
LVL 2
kintonAsked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
As you are on Net 2.0 you can use the DataView.ToTable method.  Have a look at this

http://msdn2.microsoft.com/en-us/library/h2b6ehaa.aspx

Roger
0
 
kintonAuthor Commented:
My last question was meant to read "What is the best way to create this column filter?"!
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
What is you datatable (sql, access, etc)? Or is a system datatable ?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kintonAuthor Commented:
Its a datatable object is Visual Basic 2005
0
 
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
            Next


myobject.DataSource = dt.DefaultView
0
 
SanclerCommented:
jpaulino

            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))
        dt1.Columns.Add(dc0)
        Dim dc1 As New DataColumn("Item", GetType(String))
        dt1.Columns.Add(dc1)
        dt2 = dt1
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        filltables()
        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
        dt2.Columns.RemoveAt(0)
    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 ;-)

Roger
0
 
kintonAuthor Commented:
Perfect.  Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.