How to create an html table inside a webbrowser control in winforms,table 应该显示通用列表中的对象

How to create an html table inside a webbrowser control in winforms,the table is supposed to display objects from a generic list

我的 Windows 表单中有一个 Web 浏览器控件,我想在其中显示一个 HTML table,显示通用列表中的对象(使用 [=14= 迭代] 循环)当按钮事件被调用时。这是我如何静态地做的。

我不知道如何动态地做到这一点。

这是事件:

private void rawMaterial_Ok_Click(object sender, EventArgs e)
    {
        _logger.Debug("Set Raw Materials value into Plant: " + _selectedPlant.Name);
        List<RawMaterialScenarioModel> newRMModelList = GetRawMaterialUIValues();

        _rawMaterialScenarioModelList.AddRange(newRMModelList);
        //call the getRawMaterialSetResult() to display rawmaterial values into plantSettingResultView.
        // TODO: Displaying selectd Plant and its RM Details in Result View
        this.plantSettingResultView.Refresh();
        this.plantSettingResultView.DocumentText = Utils.getRawMaterialSetResult();

我对 getRawMaterialSetResult() 函数做了一些修改,我可以根据需要显示 html table:

 public static string getRawMaterialSetResult(List<RawMaterialScenarioModel> newRMModelList, List<PlantModel> newPlantModelList)
    {
        if (newRMModelList == null || newRMModelList.Count == 0)
            return (string)null;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("<html>");
        stringBuilder.Append("<strong><br><span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\"> Result: Raw Material setting </span><br></strong> ");
        foreach (RawMaterialScenarioModel rawmaterial in newRMModelList)
        {
            stringBuilder.Append("<table width=\"99%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"MsoNormalTable\" ");
            stringBuilder.Append("style=\"width: 20.0%; mso-cellspacing: 0cm; mso-yfti-tbllook: 1184; mso-padding-alt: 0cm 0cm 0cm 0cm;\">");
            stringBuilder.Append("<tbody>");
            stringBuilder.Append("<tr style=\"mso-yfti-irow: 0; mso-yfti-firstrow: yes; height: 10.75pt;\"> ");
            stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
            stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\"> </span>");
            stringBuilder.Append("</td>");
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">PlantModel:");
            stringBuilder.Append((newPlantModelList.Find(plant => plant.Id.Equals(rawmaterial.PlantId))).Name);
            stringBuilder.Append("</th>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">Source1</th>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">Source2</th>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">Source3</th>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">RegResupply</th>");
            stringBuilder.Append("<th style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt; font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">SafetyStock</th>");
            stringBuilder.Append("</tr>");
                stringBuilder.Append("<tr style=\"mso-yfti-irow: 0; mso-yfti-firstrow: yes; height: 10.75pt;\">");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.Name);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.Source1);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.Source2);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.Source3);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.RegularResupply);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("<td style=\"border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; background: #FEFBF6; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\">");
                stringBuilder.Append("<span lang=\"EN-US\" style=\"font-size: 9.0pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family: &quot;Times New Roman&quot;; color: #4c4c4c;\">");
                stringBuilder.Append(rawmaterial.SafetyStockPeriod);
                stringBuilder.Append("</span>");
                stringBuilder.Append("</td>");
                stringBuilder.Append("</tr> ");
            }
            stringBuilder.Append("</tbody>");
            stringBuilder.Append("</table>");
            stringBuilder.Append("</html>");

            return ((object)stringBuilder).ToString();

    }