473,378 Members | 1,142 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,378 software developers and data experts.

Combining Rows

How can I easily add the rows of DataTable1 to the rows of DataTable2. Both
queries are from the same table. I can always use the column names with
myRow["name"], but I was wishing for a shortcut. When I try this it doesn’t
work.

for (int i = 0; i < dataTable1.Rows.Count; i++)
{
myRow = dataTable2.NewRow();
myRow = dataTable1.Rows[i];
dataTable2.Rows.Add(myRow);
}
return dataTable2;

Jul 21 '05 #1
3 2736
Look at the DataSet object and it's Merge function. This could be used to
combine both of these resultsets.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"Tome73" <To****@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
How can I easily add the rows of DataTable1 to the rows of DataTable2.
Both
queries are from the same table. I can always use the column names with
myRow["name"], but I was wishing for a shortcut. When I try this it doesn't
work.

for (int i = 0; i < dataTable1.Rows.Count; i++)
{
myRow = dataTable2.NewRow();
myRow = dataTable1.Rows[i];
dataTable2.Rows.Add(myRow);
}
return dataTable2;

Jul 21 '05 #2
Ben, it would seem that the merge function is mostly for unlike datasets and
the examples that are given in the SDK only addresses this issue. Furthermore
the examples go from datasets to tables to rows back to tables to rows to
datasets to tables again etc. Very hard to follow for a newbe. I was hoping
for something that took one line of code instead of fifty as with the
examples. lol I guess I will need to write a subroutine to handle it.
Something like
DataTable newDataTable = myMerge(datatable1, datatable2);

Thanks for the reply.

"Ben Lucas" wrote:
Look at the DataSet object and it's Merge function. This could be used to
combine both of these resultsets.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"Tome73" <To****@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
How can I easily add the rows of DataTable1 to the rows of DataTable2.
Both
queries are from the same table. I can always use the column names with
myRow["name"], but I was wishing for a shortcut. When I try this it doesn't
work.

for (int i = 0; i < dataTable1.Rows.Count; i++)
{
myRow = dataTable2.NewRow();
myRow = dataTable1.Rows[i];
dataTable2.Rows.Add(myRow);
}
return dataTable2;


Jul 21 '05 #3
Actually, it is really for DataSets that are more similar than different.
It provides capabilities for handling DataSets that do have differences, but
it should be easier to use if your DataSets are not different. In fact, the
docs actually state in the page for "DataSet.Merge Method (DataTable)", "The
Merge method is used to merge two DataSet objects that have largely similar
schemas."

For example, given dataTable1 and dataTable2 as provided in your original
post:

dataTable1.TableName = "myTable"; //The Data Table names should be the same
so that the merge function can match them up
dataTable2.TableName = "myTable";

DataSet ds = new DataSet();
ds.Tables.Add(dataTable1);
ds.Merge(dataTable2);

At this point, the DataSet will contain one table with the rows from both
tables.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Tome73" <To****@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
Ben, it would seem that the merge function is mostly for unlike datasets
and
the examples that are given in the SDK only addresses this issue.
Furthermore
the examples go from datasets to tables to rows back to tables to rows to
datasets to tables again etc. Very hard to follow for a newbe. I was
hoping
for something that took one line of code instead of fifty as with the
examples. lol I guess I will need to write a subroutine to handle it.
Something like
DataTable newDataTable = myMerge(datatable1, datatable2);

Thanks for the reply.

"Ben Lucas" wrote:
Look at the DataSet object and it's Merge function. This could be used
to
combine both of these resultsets.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"Tome73" <To****@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
> How can I easily add the rows of DataTable1 to the rows of DataTable2.
> Both
> queries are from the same table. I can always use the column names with
> myRow["name"], but I was wishing for a shortcut. When I try this it
> doesn't
> work.
>
> for (int i = 0; i < dataTable1.Rows.Count; i++)
> {
> myRow = dataTable2.NewRow();
> myRow = dataTable1.Rows[i];
> dataTable2.Rows.Add(myRow);
> }
> return dataTable2;
>


Jul 21 '05 #4

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

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
4
by: mike | last post by:
I have a database table like the following: id|name|item_id|sequence and the following instance data: 1|Apple|419841|1 2|Orange|419841|2 3|Banana|935890|1 4|Lime|959081|1
3
by: Tome73 | last post by:
How can I easily add the rows of DataTable1 to the rows of DataTable2. Both queries are from the same table. I can always use the column names with myRow, but I was wishing for a shortcut. When I...
5
by: Matthias Nagl | last post by:
Hello List! I would like to combine the contents of several rows of a subquery. After several hours of search in the documentation and the internet I didn'T find a solution and hope anyone can...
2
by: rpeacock | last post by:
I have a function that takes a field with values separated by commas within the field and splits them to multiple rows. Example: Field - Interior Value - abc,def,efg,ghi Output: ID Item 1 ...
5
by: KewlToyZ | last post by:
Good day, I am stuck in a strange situation. SQL 2000 Server, creating a stored procedure to use in Crystal Reports 11. I am trying to build a report without creating a table or temprorary table in...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
1
by: dlee360 | last post by:
Hello! So I've been trying to figure out how to do the following in T-SQL: Orig Table: Col1 Col2 Col3 Set A ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.