What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21585 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > Merging 2 datatable columns and bind to control

Merging 2 datatable columns and bind to control

Article posted by San.Pblr.Gct on 7/27/2012 | Views: 2069 | Category: .NET Framework | Level: Intermediate | Points: 250 red flag


In this article, let us see how to bind a combobox with different columns from different datatables. eg. I have a datatable “Server” with columns server and database. Another datatable “server1” with columns servername and database name. Now I want to display all the values in the server and server name columns into a single combobox.

Download


 Download source code for Merging 2 datatable columns and bind to control


Introduction:

In this article, let us see how to bind a combobox with different columns from different datatables.

For eg, I have a datatable “Server” with columns server and database.  Another datatable “server1” with columns servername and databasename. Now I want to display all the values in the server and servername columns into a single combobox.

Using the code:

1.      Create a new Windows application. Add a combobox  control.

2.      I have 2 xml files which am going to dump it in datasets.

XML File 1

                              <?xml version="1.0" standalone="yes" ?>

- <NewDataSet>

- <Table1>

  <Server>Server1</Server>

  <Database>Database1</Database>

  </Table1>

- <Table1>

  <Server>Server2</Server>

  <Database>Database2</Database>

  </Table1>

- <Table1>

  <Server>Server3</Server>

  <Database>Database3</Database>

  </Table1>

  </NewDataSet>


XML File 2-

<?xml version="1.0" standalone="yes" ?>

- <NewDataSet>

- <table2>

  <ServerName>Server4</ServerName>

  <DatabaseName>Database4</DatabaseName>

  </table2>

- <table2>

  <ServerName>Server5</ServerName>

  <DatabaseName>Database5</DatabaseName>

  </table2>

- <table2>

  <ServerName>Server6</ServerName>

  <DatabaseName>Database6</DatabaseName>

  </table2>

  </NewDataSet> 

3.      Now in the form_load, add the below code. I am going to merge 2 datatables from 2 datasets. Then going to create new column in the merged dataset and fill new column with values from both datatables. See the commented lines for more explanation and better understanding.

private void Form1_Load(object sender, EventArgs e)

        {

            DataSet ds = new DataSet();

            string path = "C:\\XMLFile1.xml";

            //Read XMLFILE1.XML and save it in ds

            ds.ReadXml(path);

            path = "C:\\XMLFile2.xml";

            DataSet ds2 = new DataSet();

            //Read XMLFILE2.XML and save it in ds2

            ds2.ReadXml(path);

            //Merge datatable from both datasets

            ds.Tables[0].Merge(ds2.Tables[0]);

            //add new column "All Servers"

            ds.Tables[0].Columns.Add("All Servers", typeof(string));

            //Read datatable and fetch all server and dump it into new column "All servers"

            foreach (DataRow dr in ds.Tables[0].Rows)

            {

                dr["All Servers"] = dr["server"].ToString();

            }

            //Read datatable and fetch all servername and dump it into new column "All servers"

            foreach (DataRow dr in ds.Tables[0].Rows)

            {

                //When we merged 2 datatables if there are 3 rows in datatable 1 and 2 in datatable2

                //now it will become 5rows,So am checking if its blank, then fill it with servername column from datatable2(initial)

                if (dr["All Servers"] == "")

                    dr["All Servers"] = dr["servername"].ToString();

            }

            //Bind the datatable

            comboBox1.DataSource = ds.Tables[0];

            //set displaymember as "All servers", the newly created column.

            comboBox1.DisplayMember = "All Servers";

        }

4.     Now run the application, check the combobox, you will have all the servers listed inside combobox.

I have attached complete source code also.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, April 06, 2012
Level:Starter
Status: [Member]
Biography:I am working as a developer in EF. I have been working in c#, sql, Silverlight,WPF and ASP.NET for last 3 years
>> Write Response - Respond to this post and get points
Related Posts

Steps to create Application Pool

In this article we will touch base 3 ways of doing optimistic locking i.e. Using ADO.NET dataset, SQL Server Timestamp check and old / new value check. So we will start this article with a small introduction to concurrency, discuss the 5 concurrency problems and then get down to the actual meat to understand how to implement optimistic locking using the 3 methodologies.

This articles is 1st part of the 5 part series which gives overview of the Parallel Processing in Visual Studio 2010.

The purpose of this article is to create a simple Task Management System(TMS) that will help the user to create a task, edit the task and to view the same by using MEF 4.0, WCF , Entity Framework 4.0 with some architectural patterns etc.

Service Oriented Architecture (SOA) a buzzing word in the world of software development these days. In this article, we will try to understand what the SOA is and how to create and consume it in ASP.NET with C#.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 6:29:32 AM