?
Solved

To Create DataTable From DataRow Collection

Posted on 2004-09-20
10
Medium Priority
?
4,285 Views
Last Modified: 2009-05-07
Hi All,
    Can anybody clear my doubt ,
" How Can i create datatable from a collection of datarows."
Advance Thanx
Balaji.
0
Comment
Question by:Nalinkumarbalaji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
10 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12101496
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("mHostCode", GetType(String)))
dt.Columns.Add(New DataColumn("mTotalFreq", GetType(Integer)))

Dim datar As DataRow
datar = dt.NewRow
  datar(0) = MyDataRow1Value
  datar(1) = MyDataRow2Value
  dt.Rows.Add(datar)

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12101500
I usually accomplish this with a datareader though:

Dim dt As New DataTable
dt.Columns.Add(New DataColumn("mHostCode", GetType(String)))
dt.Columns.Add(New DataColumn("mTotalFreq", GetType(Integer)))

...do your sql connection and command decleration...

dr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
Dim datar As DataRow
While dr.Read
  datar = dt.NewRow
  datar(0) = dr(0)
  datar(1) = dr(1)
  dt.Rows.Add(datar)
End While
dr.Close
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12101571
Hello Balaji,

In following example, I am creating DataTable from DataRow.

----------------------------------------------------------------------------------------------------------------

DataTable dt = new DataTable();
DataView dv;
DataRow dr;
      
                dt.Columns.Add( new DataColumn("Column0",Type.GetType("System.String") ) );
      dt.Columns.Add( new DataColumn("Column1",Type.GetType("System.String") ) );
                        
      dr = dt.NewRow();                                                  //Create 1st Row
      dr[0] = "row1:Column0";
      dr[1] = "row1:Column1";
      
      dt.Rows.Add(dr);                                                   // Add 1st row to table
               
                    dr = dt.NewRow();                                                 //Create 2nd Row
      dr[0] = "row2:Column0";
      dr[1] = "row2:Column1";
      dt.Rows.Add(dr);                                                   // Add 2nd row to table

      dv = new DataView( dt );

----------------------------------------------------------------------------------------------------------------


-tushar
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Snarf0001
ID: 12114277
If the rows already belong to a datatable, if you're getting them from a DataTable.Select for example, you need to use

  myDataTable.ImportRow(myDataRow)
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12333580
Mine and AerosSaga's code does exactly what user is asking for. But the points should go to AerosSaga as he showed 2 correct alternative method before me.

-tushar
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12333746
thank you tushar...I happily agree....Aeros
0
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 500 total points
ID: 12334751
The code you provided does not necessarily answer exactly what the user asked for.  He is asking to make a datatable from a pre-existing collection of datarows.  The code provided creates new individual rows one at a time, and then adds them to the table.

The only situation I've come across like this personally, is when I need to make a "sub" datatable from a DataTable.Select(...) result.  If this is in fact the case for this user (which I'm speculating on), Rows.Add directly from the resulting array will fail, because the rows already belong to another DataTable.  ImportRow would have to be used.  It would still of course be possible to create the structure, a new row, and add all of the values and individual rows manually, but .ImportRow would yeild significantly better performance.

The structure would still have to be copied over, but rather than creating column by column, a simple DataTable.Clone() statement can be used from the original table.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

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