Merging two Datatables into a Matrix style Datatable.

37 views
Skip to first unread message

Tim Riley

unread,
Dec 5, 2007, 2:22:19 PM12/5/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I have a situation where I need to take multiple DataTables and
convert them to a single Datatable which would be simple if I didn't
need to convert them into a matrix where like items will be combined.
For example say I have 3 DataTables like so:

Note all three datatables have the same fields. They are:
Qty, CatalogNo, Part, Description, length, width

DataTable1 with comma seperated field values:
1, Q123456, 01, Limit Switch, 0, 0
1, E654321, 02, 1/2" Bolt, 0, 0
8, F786543, 03, 3/4" Bolt, 0, 0
1, P967349, 04, 1/2" Stainless Plate, 13, 14
1, P967349, 05, 1/2" Stainless Plate, 123, 48

DataTable2 with comma seperated field values:
1, Q123456, 01, Limit Switch, 0, 0
1, E654321, 02, 1/2" Bolt, 0, 0
8, F786543, 03, 3/4" Bolt, 0, 0
1, P967322, 04, 1/2" Carbon Plate, 13, 14
1, P967322, 05, 1/2" Carbon Plate, 123, 48

DataTable3 with comma seperated field values:
1, Q123456, 01, Limit Switch, 0, 0
3, E654321, 02, 1/2" Bolt, 0, 0
8, F786543, 03, 3/4" Bolt, 0, 0
1, P967349, 04, 1/2" Stainless Plate, 13, 14
1, P967349, 05, 1/2" Stainless Plate, 123, 48


These tables would be then converted into a table with the fields:

Qty03, Qty02, Qty01, CatalogNo, Part, Description, length, width

And have the comma seperated field values of:
1, 1, 1, Q123456, 01, Limit Switch, 0, 0
3, 1, 1, E654321, 02, 1/2" Bolt, 0, 0
8, 8, 8, F786543, 03, 3/4" Bolt, 0, 0
1, 0, 1, P967349, 04, 1/2" Stainless Plate, 13, 14
0, 1, 0, P967322, 04, 1/2" Carbon Plate, 13, 14
1, 0, 1, P967349, 05, 1/2" Stainless Plate, 123, 48
0, 1, 0, P967322, 05, 1/2" Carbon Plate, 123, 48

I realize this isn't really clear but I hope someone can follow what
I'm trying to do. I'll take any help I can get.

Thanks,
Tim

Michael O'Neill

unread,
Dec 6, 2007, 10:37:30 AM12/6/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
There's an extremely scalable solution out there, all the rage:
http://oracle.com , foregoing that you could just use a mess of for
loops - what's stopping you from that?

Tim Riley

unread,
Dec 6, 2007, 12:41:26 PM12/6/07
to DotNetDe...@googlegroups.com
I don't see how Oracle would really help me out as these datatables
aren't interfacing with a database at all. They are all being
generated from a DataGridView in a winform where users enter data.
They are then being stored in XML format as part of a CAD drawing.

For loops is the approach I'm taking now, however I was wondering if
there is a more elegant solution out there that I'm not getting. If
I'm running for loops across several decent sized data tables I'm
afraid it will slow my app down.

~Tim

Andrew Badera

unread,
Dec 6, 2007, 2:02:42 PM12/6/07
to DotNetDe...@googlegroups.com
Have you considered LINQ?

--Andrew Badera

Peter Groenewegen

unread,
Dec 6, 2007, 3:23:30 PM12/6/07
to DotNetDe...@googlegroups.com
Posting this takes more time as just writing the code:
sort, and merge your table and you are done.
 
if you need more performance, use a hash table (no sorting and searching needed).
 
--
Peter

Michael O'Neill

unread,
Dec 6, 2007, 8:13:51 PM12/6/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I really love that you answered your own questions.

A more elegant solution? a database server
A more scalable solution? a database server

Sure, if you've only got the minuscule amounts of data you've
presented in the original post then utilizing a database server would
be overkill, but I suspect you are only revealing only the tip of the
iceberg.

Database servers aren't just persistent heap managers. They are
extraordinary set processing tools. Your issue is a set processing
matter; I'm just suggesting that you use the right tool for the right
job.

Good luck.

On Dec 6, 11:41 am, "Tim Riley" <ril...@gmail.com> wrote:
> I don't see how Oracle would really help me out as these datatables
> aren't interfacing with a database at all. They are all being
> generated from a DataGridView in a winform where users enter data.
> They are then being stored in XML format as part of a CAD drawing.
>
> For loops is the approach I'm taking now, however I was wondering if
> there is a more elegant solution out there that I'm not getting. If
> I'm running for loops across several decent sized data tables I'm
> afraid it will slow my app down.
>
> ~Tim
>
> On Dec 6, 2007 10:37 AM, Michael O'Neill <cleveridea....@gmail.com> wrote:
>
>
>
> > There's an extremely scalable solution out there, all the rage:
> >http://oracle.com, foregoing that you could just use a mess of for

Tim Riley

unread,
Dec 6, 2007, 10:42:16 PM12/6/07
to DotNetDe...@googlegroups.com
I never answered my own question. A database server is not an option
at all. As I said before the entire DataSet that contains the
DataTables are stored as a XML record in an AutoCAD drawing file's
internal database.

Data storage is not an issue for me at all. It's taking the data from
a multiple DataTables and combining them into a Matrix which I can
then draw the geometry on the drawing via code.

~tim

Andrew Badera

unread,
Dec 8, 2007, 7:55:54 AM12/8/07
to DotNetDe...@googlegroups.com
Your "Matrix" is really just another data table. You're going to have to build a new DataTable, row by row, cell by cell. It's a bit tedious, but the final DataTable (or Grid if you build that instead) should represent what you're looking for.

Also, again, have you considered LINQ?

--Andrew Badera
Reply all
Reply to author
Forward
0 new messages