Solved

How to fill a datatable from a datatable

Posted on 2016-10-04
8
47 Views
Last Modified: 2016-10-25
I want to create a datatable (ResultDataTable) in vb.net with a subset of columns and a subset of rows from another datatable (SourceDataTable) so I could pass ResultDataTable to a SQL Server stored procedure.  If  SourceDataTable has columns C1, C2, and C3, how can I create a ResultDataTable that only has the column C1 and only has rows where C3=1?  The SQL Statement from ResultDataTable would be "SELECT C1 from SourceDataTable WHERE C3 = 1".
0
Comment
Question by:Declan_Basile
8 Comments
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 125 total points (awarded by participants)
ID: 41828622
You can create a DataView from your existing table that filters the rows based on your criteria. Once the DataView object is created, you can use the ToTable() methods to create a new table. One of the overloaded methods for ToTable takes a string array of column names you want in the table.

Below is a link to the ToTable method of the DataView object:
https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx
0
 
LVL 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 125 total points (awarded by participants)
ID: 41828628
Your selection result should have the .CopyToDataTable() method.  So for example

ResultDataTable = SelectionResult.CopyToDataTable()
0
 
LVL 11

Accepted Solution

by:
ROMA CHAUHAN earned 250 total points (awarded by participants)
ID: 41829658
Hi Declan_Basile,

I think solution provided in the link shared by Shaun Kline, will not give you the exact solution for which you are looking.

You can try out the below method. I referred the same link but made some changes as per your need.

Private Sub GetResultTableFrmSrcTable()

        Dim SourceDataTable As DataTable = New DataTable("SourceDataTable")

        Dim column As DataColumn = New DataColumn("C1", GetType(System.String))
        SourceDataTable.Columns.Add(column)

        column = New DataColumn("C2", GetType(System.String))
        SourceDataTable.Columns.Add(column)

        column = New DataColumn("C3", GetType(System.Int32))
        SourceDataTable.Columns.Add(column)


        ' Add some items.
        Dim row As DataRow = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Apple", "Fruit", 1}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Orange", "Fruit", 1}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Muffin", "Bread", 2}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Salmon", "Fish", 3}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Salmon", "Fish", 3}
        SourceDataTable.Rows.Add(row)

        row = SourceDataTable.NewRow()
        row.ItemArray = New Object() {"Croissant", "Bread", 2}
        SourceDataTable.Rows.Add(row)

        ' Filter source table where C3=1
        SourceDataTable.DefaultView.RowFilter = "C3=1"

        'Get C1 column from dataview
        Dim ResultDataTable As DataTable = SourceDataTable.DefaultView.ToTable(False, "C1")

    End Sub

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:Declan_Basile
ID: 41831752
Thanks everyone.  I tried both methods and both worked.  I went with the Dataview method because I found it easier.  Thank you Roma for making it even easier by providing the example.
0
 
LVL 1

Author Comment

by:Declan_Basile
ID: 41852367
I see a close request, but I thought that I already assigned points and closed this question.  Also, I don't see any "Accept as Answer" buttons.  Can someone please advise?
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41852457
Ah, the question was left open, so it notifies experts to suggest a closing to the question to keep the queue of open questions managed.  

The question will auto-close itself once an expert has made a suggestion for closing- but as the author if you want to change the distribution of points or select just one answer, open a new comment and instead of clicking 'Submit' choose 'Object'.  

I'm unsure if when the author objects it will automatically accept the results, but if not then the question will auto-close after a few days with whatever your new solution selection is.
0
 
LVL 1

Author Comment

by:Declan_Basile
ID: 41854457
OK.  I'll just let this auto-close then.  Thanks.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41858243
Author suggested both approaches worked, cited the answer from Roma to be the most robust.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question