Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Merge two different DataTable columns and bind to a control

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Jul 2012CPOL 22.5K   93   6  
How to bind a combobox with different columns from different DataTables.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MergetwoDatatableColumns
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead EF (Education First)
India India
I graduated as Production Engineer and started my career as Software Developer then worked as tester for a while before moving into Windows application development using Microsoft Technologies. But for the last few years i am working on javascript, React, Node, AWS, Azure Chatbots

Comments and Discussions