Разбор HTML StringBuilder в элемент управления WebBrowser с помощью C #

У меня такой код:

private StringBuilder htmlMessageBody(DataGridView dataGridView2)
{
    StringBuilder strB = new StringBuilder();
    //create html & table
    strB.AppendLine("<html><body><center><" +
                    "table border='1' cellpadding='0' cellspacing='0'>");
    strB.AppendLine("<tr>");
    //cteate table header
    for (int i = 0; i < dataGridView2.Columns.Count; i++)
    {
        strB.AppendLine("<td align='center' valign='middle'>" +
                        dataGridView2.Columns[i].HeaderText + "</td>");
    }
    //create table body
    strB.AppendLine("<tr>");
    for (int i = 0; i < dataGridView2.Rows.Count; i++)
    {
        strB.AppendLine("<tr>");
        foreach (DataGridViewCell dgvc in dataGridView2.Rows[i].Cells)
        {
            strB.AppendLine("<td align='center' valign='middle'>" +
                            dgvc.Value.ToString() + "</td>");
        }
        strB.AppendLine("</tr>");

    }
    //table footer & end of html file
    strB.AppendLine("</table></center></body></html>");
    return strB;


}

Как мне вызвать его, чтобы он отображался в элементе управления веб-браузера через событие щелчка на кнопке?


person PriceCheaperton    schedule 12.01.2014    source источник


Ответы (1)


Установите свойство DocumentText на созданный HTML. Обратите внимание, что вы возвращаете StringBuilder от htmlMessageBody, поэтому вам нужно будет позвонить ToString, чтобы получить текст

webBrowser.DocumentText = htmlMessageBody(theDataGridView).ToString();
person JaredPar    schedule 12.01.2014