Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6140
  • Last Modified:

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?
0
kinton
Asked:
kinton
  • 3
  • 2
  • 2
1 Solution
 
kintonAuthor Commented:
My last question was meant to read "What is the best way to create this column filter?"!
0
 
jpaulinoCommented:
What is you datatable (sql, access, etc)? Or is a system datatable ?
0
 
kintonAuthor Commented:
Its a datatable object is Visual Basic 2005
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SanclerCommented:
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
 
jpaulinoCommented:
           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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now