Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2007, 10:04 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataSet - Merge Data Tables

I have a dataset with the following data

GRP_ID RATE_DESC RATE_TYPE_ID RATE
6 Rate 1 1 5.350
6 Rate 2 11 14.450
6 Rate 3 12 18.350
2 Rate 1 1 4.250
2 Rate 2 11 22.450
2 Rate 3 12 22.250

I need to display the above in the following way:

RATE_DESC RATE_TYPE_ID RATE RATE
Rate 1 1 5.350 4.250
Rate 2 11 14.450 22.450
Rate 3 12 18.350 22.250


I used the following code, but doesn't like it:

                DataView dv1 = new DataView();
                dv1 = resultDS.Tables["0"].DefaultView;
                dv1.RowFilter = "grp_id = 6 ";
                DataTable dtFirst = new DataTable();
                dtFirst = dv1.Table;

                DataView dv2 = new DataView();
                dv2 = resultDS.Tables["0"].DefaultView;
                dv2.RowFilter = "grp_id = 2 ";

                DataTable dtSecond = new DataTable();
                dtSecond = dv2.Table;

                for (int i = 0; i < dtSecond.Columns.Count; i++)
                {
                    dtSecond.Columns[i].ColumnName = "C" + i.ToString();
                }


                DataSet ds1 = MergeTables(dtFirst, dtSecond);
                rptrRateHistory.DataSource = ds1;
                rptrRateHistory.DataBind();


        private DataSet MergeTables(DataTable dtFirst, DataTable dtSecond)
        {
            DataSet resultDS = new DataSet();
            DataTable dtResults = dtFirst.Clone();
            int count = dtSecond.Columns.Count;

            for (int i = 0; i < count; i++)
            {
                if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
                {
                    dtResults.Columns.Add(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
                }
            }


            for (int i = 0; i < dtFirst.Rows.Count; i++)
            {
                object[] rowFirst;
                rowFirst = dtFirst.Rows[i].ItemArray;

                int j = 0;
                object[] rowMerged = new object[dtResults.Columns.Count];
                foreach (object obj in rowFirst)
                {
                    rowMerged[j] = obj;
                    j++;
                }

                DataRow row = dtResults.NewRow();
                row.ItemArray = rowMerged;
                dtResults.Rows.Add(row);
            }

            resultDS.Tables.Add(dtResults);
            return resultDS;
        }

Do you guys have any idea, how to accomplish this....

The problem i'm facing is both the datatables have the same columns name and I need to get the RATE Column from the second table and merge it with the first table, so that I can display in the format I'm finally looking for...

any suggestions please.....
Thanks in advance!






Similar Threads
Thread Thread Starter Forum Replies Last Post
merge data in tables-access 2003 megank Access 2 April 5th, 2006 11:31 AM
Whether or not to merge two tables chrislepingwell SQL Language 1 November 24th, 2005 06:18 PM
How to Merge Tables into one query sorcerer Access 5 March 11th, 2005 04:19 AM
Dataset Merge then DB Update druid2112 ADO.NET 0 October 4th, 2004 08:44 AM
Dataset Merge Problem jpitzer ASP.NET 1.0 and 1.1 Basics 1 January 9th, 2004 02:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.