Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two datatables datatable1 & datatable2.

datatable1 contains heading with 3 rows
SL.No CutomerId CustomerName PaymentId  
1,12345,ABC,99999 
2,23456,FGH,88888 
3,54321,MNB,77777


datatable2 Contains heading with 2 rows
BankRef BankName PaymentId Address City  
ref12, HSBC, 99999, Street11, NY 
ref13, HSBC, 88888, Street11, NY


My requirement is combination both tables with only selected columns in html string format as below(heading & rows). That is 3 columns from datatable2 & 1 column from datatable1.

BankRef BankName CustomerName PaymentId
ref12,   HSBC,    ABC,       99999 


How to achieve this.
Thanks in advance.

What I have tried:

How to combain these two datatables and show in one
Posted
Updated 1-Jul-16 22:36pm
v3
Comments
ZurdoDev 1-Jul-16 14:25pm    
Use a grid, write sql to select from both tables and get what you want, then bind your grid to the data. Where are you stuck?

Here is the ready soup for you.just use and feel free if you have any question

C#
    class Program
    {
       static DataTable dtCustomer = new DataTable();
       static DataTable dtPaymentInfo = new DataTable();
        static DataTable dtResults = new DataTable();
        static void Main(string[] args)
        {
            FillDataTables();
            var t = from paymentinfo in dtPaymentInfo.AsEnumerable()
                    join customer in dtCustomer.AsEnumerable()
                    on paymentinfo.Field<int>("PaymentId") equals customer.Field<int>("PaymentId")
                    select new { BankRef = paymentinfo.Field<string>("BankRef") , BankName = paymentinfo.Field<string>("BankName"), CustomerName = customer.Field<string>("CustomerName"), PaymentId = paymentinfo.Field<int>("PaymentId") };
            dtResults.Columns.Add("BankRef", typeof(string));
            dtResults.Columns.Add("BankName", typeof(string));
            dtResults.Columns.Add("CustomerName", typeof(string));
            dtResults.Columns.Add("PaymentId", typeof(int));
            foreach (var r in t)
            {
                dtResults.Rows.Add(r.BankRef,r.BankName,r.CustomerName,r.PaymentId);
            }

            string html = ConvertDataTableToHTML(dtResults);
        }
        public static string ConvertDataTableToHTML(DataTable dt)
        {
            string html = "<table border="\"1\"">";
            //add header row
            html += "<tr>";
            for (int i = 0; i < dt.Columns.Count; i++)
                html += "<td>" + dt.Columns[i].ColumnName + "</td>";
            html += "</tr>";
            //add rows
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<tr>";
                for (int j = 0; j < dt.Columns.Count; j++)
                    html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
                html += "</tr>";
            }
            html += "</table>";
            return html;
        }

        static void FillDataTables()
        {
            //To fill data with your sample values for testing purpose
            //SL.No CutomerId CustomerName PaymentId
            dtCustomer.Columns.Add("SL.No", typeof(int));
            dtCustomer.Columns.Add("CutomerId", typeof(int));
            dtCustomer.Columns.Add("CustomerName", typeof(string));
            dtCustomer.Columns.Add("PaymentId", typeof(int));

            dtCustomer.Rows.Add(1, 12345, "ABC", 99999);
            dtCustomer.Rows.Add(2, 23456, "FGH", 88888);
            dtCustomer.Rows.Add(3, 54321, "MNB", 77777);

            //BankRef BankName PaymentId Address City  

            dtPaymentInfo.Columns.Add("BankRef",typeof(string));
            dtPaymentInfo.Columns.Add("BankName", typeof(string));
            dtPaymentInfo.Columns.Add("PaymentId", typeof(int));
            dtPaymentInfo.Columns.Add("Address", typeof(string));
            dtPaymentInfo.Columns.Add("City", typeof(string));

            dtPaymentInfo.Rows.Add("ref12", "HSBC", 99999, "Street11", "NY");
            dtPaymentInfo.Rows.Add("ref13", "HSBC", 88888, "Street11", "NY");

        }
    }
</int></string></string></string></int></int>
 
Share this answer
 
use this function

C#
public static string ConvertDataTableToHTML(DataTable dt)
    {
        string html = "<table>";
        //add header row
        html += "<tr>";
        for(int i=0;i<dt.columns.count;i++)>
            html+="<td>"+dt.Columns[i].ColumnName+"</td>";
        html += "</tr>";
        //add rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            html += "<tr>";
            for (int j = 0; j< dt.Columns.Count; j++)
                html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }


And you can modify to according to your requirement
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900