Datatable to html Table

前端 未结 8 2036
感动是毒
感动是毒 2020-11-27 14:42

I have question, that maybe someone here wouldn\'t mind to help me with. I have lets say 3 datatables, each one of them has the following columns:

size, quantity, amo

8条回答
  •  醉梦人生
    2020-11-27 15:26

    The first answer is correct, but if you have a large amount of data (in my project I had 8.000 rows * 8 columns) is tragically slow.... Having a string that becomes that large in c# is why that solution is forbiden

    Instead using a large string I used a string array that I join at the end in order to return the string of the html table. Moreover, I used a linq expression ((from o in row.ItemArray select o.ToString()).ToArray()) in order to join each DataRow of the table, instead of looping again, in order to save as much time as possible.

    This is my sample code:

    private string MakeHtmlTable(DataTable data)
    {
                string[] table = new string[data.Rows.Count] ;
                long counter = 1;
                foreach (DataRow row in data.Rows)
                {
                    table[counter-1] = "" + String.Join("", (from o in row.ItemArray select o.ToString()).ToArray()) + "";
    
                    counter+=1;
                }
    
                return "
    " + String.Join("", table) + "
    "; }

提交回复
热议问题