本文介绍了如何将数据表转换为HTML格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据表datatable1& datatable2。



datatable1包含3行标题

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包含2行标题



datatable2 Contains heading with 2 rows

BankRef BankName PaymentId Address City  
ref12, HSBC, 99999, Street11, NY 
ref13, HSBC, 88888, Street11, NY





我的要求是组合两个表格,只有html字符串格式的选定列如下(标题和行) 。这是来自datatable2&的3列。来自datatable1的1列。





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

推荐答案

    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>


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


这篇关于如何将数据表转换为HTML格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 13:37