簡體   English   中英

C#WinForm的DataGridView數據到html

[英]DataGridView data of C# WinForm to html

一些數據顯示在datagridview(Winform)中。 我需要以類似的格式(行,列等)郵寄以datagridview表示的表。 我最容易做到的是什么? 如何使用列標題將其轉換為html表並保留列寬。

我不知道是否可以進行任何框架級別的轉換。 但是,遍歷網格的行和單元格,並在適當的Width位置用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>");

我認為,如果您進行搜索,就會在網絡上找到它

如果你不...

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.

快速Google搜索產生了這樣的結果: 如何通過電子郵件發送DataGridView內容 它涵蓋了將DGV轉換為HTML表並將其郵寄的過程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM