Parsing StringBuilder HTML to WebBrowser control using C#

南笙酒味 提交于 2019-12-25 02:13:17

问题


I have the following code:

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;


}

How do i call it so that it shows up in a web browser control via a click event on a button?


回答1:


private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.DocumentText = htmlMessageBody(yourdataGridView).ToString();
}



回答2:


Set the DocumentText property to the created HTML. Note that you are returning a StringBuilder from htmlMessageBody so you will need to call ToString to get the text

webBrowser.DocumentText = htmlMessageBody(theDataGridView).ToString();


来源:https://stackoverflow.com/questions/21077783/parsing-stringbuilder-html-to-webbrowser-control-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!