Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 8th, 2007, 10:04 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
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!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:05 AM.


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