By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Combining Rows

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.