Combining Rows

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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;
dataTable2.Rows.Add(myRow);
}
return dataTable2;
 
Look at the DataSet object and it's Merge function. This could be used to
combine both of these resultsets.
 
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 said:
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 said:
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;
dataTable2.Rows.Add(myRow);
}
return dataTable2;

 
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 said:
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 said:
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 said:
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;
dataTable2.Rows.Add(myRow);
}
return dataTable2;

 
Back
Top