代码之家  ›  专栏  ›  技术社区  ›  blitzkriegz

C#WinForm的DataGridView数据转换为html

c#
  •  0
  • blitzkriegz  · 技术社区  · 17 年前

    3 回复  |  直到 17 年前
        1
  •  1
  •   Chris Doggett    17 年前

    我不知道是否可以进行任何框架级别的转换。但迭代网格行和单元格,并在HTML中重新创建宽度,应该是一件非常简单的事情。

    即兴写的,请原谅它的不完整和任何简单的错误,但我认为你可以在这里得到基本的想法。

    StringBuilder sb = new SringBuilder();
    
    sb.AppendLine("<table>");
    sb.AppendLine("  <tr>");
    foreach(DataGridViewColumn column in grid.Columns)
    {
      sb.AppendLine("    <td>" + column.HeaderText + "</td>");
    }
    sb.AppendLine("  </tr>");    
    
    foreach(DataGridViewRow row in grid.Rows)
    {
      sb.AppendLine("  <tr>");
      foreach(DataGridViewCell cell in row.Cells)
      {
        sb.AppendLine("    <td width=\"" + cell.Width + "px\">" + cell.Value + "</td>");
      }
      sb.AppendLine("  </tr>");
    }
    sb.AppendLine("</table>");
    
        2
  •  2
  •   Matthew Vines    17 年前

    快速谷歌搜索得出了以下结果: How to send DataGridView contents by email

        3
  •  1
  •   Shafqat Ahmed    17 年前

    我想如果你搜索一下,你会在网上找到这个

    如果你不这样做。..

    Do the following 
    1. Create a stringbuilder, wrap it with htmlTextWriter
    2. Create a htmltable, add a header row, 
    3. Iterate through the gridheaders and add cells, write the width in attributes, write other stuff like font, fontsize, forecolor, backcolor etc if you want
    4. Next iterate through the grid rows, add a row each time to table, add cells to row for each grid cells
    5. Call the render method passing the html writer
    6. Take the string builder out and you have a html table layout output.