【问题标题】:Parsing StringBuilder HTML to WebBrowser control using C#使用 C# 将 StringBuilder HTML 解析为 WebBrowser 控件
【发布时间】:2014-01-12 17:21:42
【问题描述】:

我有以下代码:

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;


}

如何调用它,以便它通过按钮上的单击事件显示在 Web 浏览器控件中?

【问题讨论】:

    标签: c# winforms webbrowser-control stringbuilder


    【解决方案1】:
    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.DocumentText = htmlMessageBody(yourdataGridView).ToString();
    }
    

    【讨论】:

      【解决方案2】:

      DocumentText 属性设置为创建的HTML。请注意,您从 htmlMessageBody 返回 StringBuilder,因此您需要调用 ToString 来获取文本

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多