Solved

To Create DataTable From DataRow Collection

Posted on 2004-09-20
10
4,248 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
  • 3
  • 2
  • 2
10 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 125 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

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 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL best practice question 3 36
Any Third party toll for Integrating chat system on ASP.NET ? 2 34
Error in query expression 3 35
Message not shown 5 33
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now