简体   繁体   中英

C# Convert 2D array to html table

I have a 2 dimensional string array and i want to convert it to a html table. When i am running the below code it taking a lot of time because of the huge length of array.

  string data = string.Empty;
        string table = string.Empty;

        #region new code
        data += "<div class=\"tab-content\">";


        table = " <table class=\"table data-table\">";
        table += "<tbody>";
        for (int row = 1; row < Data.GetLength(0); row++)
        {
            table += "<tr>";
            for (int column = 1; column < Data.GetLength(1); column++)
            {
                try
                {
                    table += "<td>" + Data[row, column].ToString() + "</td>";
                }
                catch
                {
                    table += "<td></td>";
                }
            }
            table += "</tr>";
        }
        table += "</tbody>";


        table += "</table>";

Try to use a StringBuilder . When you do it that way, you are creating multiple string instances because strings are immutable

Depending on how often that try { } blows, I'd try to diagnose whether the indices are legal beforehand. Try-Catch is really expensive when it happens a lot.

[Edit:] Wait, it shouldn't be the indices, you are retrieving the bounds. Is this Try-Catch a null-check substitute?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM