本文介绍了使用 C# 将 StringBuilder HTML 解析为 WebBrowser 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
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 浏览器控件中?
How do i call it so that it shows up in a web browser control via a click event on a button?
推荐答案
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.DocumentText = htmlMessageBody(yourdataGridView).ToString();
}
这篇关于使用 C# 将 StringBuilder HTML 解析为 WebBrowser 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!