Solved

To Create DataTable From DataRow Collection

Posted on 2004-09-20
10
4,260 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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