473,480 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2745
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
8333
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
3231
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
1946
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
338
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
1721
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
2535
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
2502
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
2818
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
1617
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 ...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
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
7092
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
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.