Click here to Skip to main content
13,556,396 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 30 Jul 2012
Licenced CPOL

Merge two different DataTable columns and bind to a control

Rate this:
Please Sign up or sign in to vote.
How to bind a combobox with different columns from different DataTables.


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

For example, I have a DataTableServer” with columns Server and Database. Another DataTableserver1” with columns Servername and Databasename. Now I want to display all the values in the Server and Servername columns in a single combobox.

Using the code

  1. Create a new Windows application. Add a combobox control.
  2. I have two XML files which I am going to dump in DataSets.
  3. XML file 1
    <?xml version="1.0" standalone="yes" ?> 
    - <NewDataSet>
    - <Table1>
    - <Table1>
    - <Table1>
    XML file 2
    <?xml version="1.0" standalone="yes" ?> 
    - <NewDataSet>
    - <table2>
    - <table2>
    - <table2>
  4. Now in form_load, add the below code. I am going to merge two DataTables from two DataSets. Then I am going to create a new column in the merged DataSet and fill the new column with values from both DataTables. See the commented lines for more explanation and better understanding.
  5. 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
        path = "C:\\XMLFile2.xml";
        DataSet ds2 = new DataSet();
        //Read XMLFILE2.XML and save it in ds2
        //Merge datatable from both datasets
        //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";
  6. Now run the application, check the combobox, and you will have all the servers listed inside the combobox.

I have attached the complete source code also.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Santhosh Kumar Jayaraman
Software Developer Radiometer
India India
Currently i am trying to learn all about javascripts . I have experience in Microsoft technologies like WPF, WCF, ASPNET, WinForms,Silverlight, VB.NET, C-Sharp, Entity framework,SSRS, LINQ, Extension methods and SQL server. I started with my career in Infosys in the year 2009 and i worked with Trigent, Education First (EF) in the recent years before joining my current company Radiometer.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Santhosh Kumar Jayaraman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid