Solved

To Create DataTable From DataRow Collection

Posted on 2004-09-20
10
4,241 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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‚Ķ
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

15 Experts available now in Live!

Get 1:1 Help Now