473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

DataTable Copy?

I have a datatable as a source and I need to produce a datatable which
is all the rows in the original datatable except the first row, copied n
times. So, if I had a datatable of:

A B C
1 21 12 32
2 1 43 12
3 32 12 35

I need to output:

A B C
1 1 43 12
2 32 12 35
3 1 43 12
4 32 12 35
5 1 43 12
6 32 12 35

can anyone supply sample code?
thanks!

Nov 15 '05 #1
3 4721

Hi Matthew,

Thank you for posting in the community!

Based on my understanding, you want to do some row based custom copy in
datatable. You want some sample code.

================================
I think you can use DataTable.ImportRow method to import a row into the new
datatable.

Do like this:
DataTable dt;
private void constructdatatable()
{
dt=new DataTable();

dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");

DataRow dr=dt.NewRow();
dr["column1"]=21;
dr["column2"]=12;
dr["column3"]=32;
dt.Rows.Add(dr);

dr=dt.NewRow();
dr["column1"]=1;
dr["column2"]=43;
dr["column3"]=12;
dt.Rows.Add(dr);

dr=dt.NewRow();
dr["column1"]=32;
dr["column2"]=12;
dr["column3"]=35;
dt.Rows.Add(dr);
}

private void button_Click(object sender, System.EventArgs e)
{
DataTable dtnew;
dtnew=dt.Clone();
for(int i=0;i<3;i++)
{
dtnew.ImportRow(dt.Rows[1]);
dtnew.ImportRow(dt.Rows[2]);
}

dataGrid1.DataSource=dtnew;
}

=======================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2
It did help. I also need to copy a series of columns (and their data) n
times, and that seems to be more difficult. Given:
A B C
1 21 12 32
2 1 43 12
3 32 12 35

I need to produce:
A B C D E F G
1 21 12 32 12 32 12 32
2 1 43 12 43 12 43 12
3 32 12 35 12 35 12 35
Jeffrey Tan[MSFT] wrote:
Hi Matthew,

Thank you for posting in the community!

Based on my understanding, you want to do some row based custom copy in
datatable. You want some sample code.

================================
I think you can use DataTable.ImportRow method to import a row into the new
datatable.

Do like this:
DataTable dt;
private void constructdatatable()
{
dt=new DataTable();

dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");

DataRow dr=dt.NewRow();
dr["column1"]=21;
dr["column2"]=12;
dr["column3"]=32;
dt.Rows.Add(dr);

dr=dt.NewRow();
dr["column1"]=1;
dr["column2"]=43;
dr["column3"]=12;
dt.Rows.Add(dr);

dr=dt.NewRow();
dr["column1"]=32;
dr["column2"]=12;
dr["column3"]=35;
dt.Rows.Add(dr);
}

private void button_Click(object sender, System.EventArgs e)
{
DataTable dtnew;
dtnew=dt.Clone();
for(int i=0;i<3;i++)
{
dtnew.ImportRow(dt.Rows[1]);
dtnew.ImportRow(dt.Rows[2]);
}

dataGrid1.DataSource=dtnew;
}

=======================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 15 '05 #3

Hi Matthew,

Thanks very much for your feedback.

I am glad it works.

For your further concern, I viewed that you have figured it out to use
DataTable.Columns.AddRange method. Please follow up there.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian Bischof | last post by:
I'm using a third-party tool that takes a DataTable as a parameter. I really need to pass it a DataView instead. When I try to explicitly cast the DataView as a DataTable I get an error that they...
5
by: Stefan Turalski \(stic\) | last post by:
Hi, I'm wondering if there is a way to send a method parametrs by ref when DataTabel is a type of this value ? I done some sort of select over DataTable columns, just by removing them froma...
3
by: VM | last post by:
How can I copy the contents of one datatable to another datatable? I've already the source table so I only need to copy its rows. Thanks.
0
by: Bennett Haselton | last post by:
If I have just filled a DataTable in a typed DataSet with a single row, is there a way I can make a copy of that row, so that I can clear the DataTable in that DataSet and use it for another query,...
3
by: Gene Hubert | last post by:
I'm using DataTable.ImportRow to move data from one datatable to another... Dim dt, dtTarget As DataTable Dim dr As DataRow dt = DirectCast(Me.DataSource, DataTable) dtTarget = dt.Clone...
12
by: Doug Bell | last post by:
Hi, I am having problems trying to create a (temporary) DataTable from a selection from a DataGrid (dgOrders). dtOrdDetails is declared as a Public DataTable Sub is: Dim stFilter as String...
3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
4
by: Férnas | last post by:
Hey guys, In my app, I have a DataTable named "dta"... If i create a new variable of DataTable type named "dta2", and assign the dta as value, it creates a reference to dta, then, when I make...
0
by: mikejacobz | last post by:
Hi, I have a Datatable which I would like to copy into two other DataTables. The first copy of the DataTable would contain the first 1/2 of the original DataTable whilst the second DataTable...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.