[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

To Create DataTable From DataRow Collection

Posted on 2004-09-20
10
Medium Priority
?
4,294 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
Industry Leaders: 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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