2009-06-02 3 views
0

일부 데이터는 DataGridview (Winform)에 표시됩니다. datagridview에 표시된 테이블을 비슷한 형식 (행, 열 등)으로 메일로 보내기 요구 사항이 있습니다. 내가 할 수있는 가장 쉬운 방법은 무엇입니까? 열 머리글을 사용하여 html 테이블로 변환하고 열 너비를 유지하는 방법.DataGridView C# WinForm의 데이터를 html로

+1

참고 : http://stackoverflow.com/questions/16008477/export-datagridview-to-html-page –

답변

2

발생할 수있는 프레임 워크 수준 변환이 있는지 알 수 없습니다. 그러나 그리드 행과 셀을 반복하고 너비가있는 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>"); 
1

당신이 그렇게하지 않으면 당신이 웹

에이를 찾을 것입니다 검색하면 내 생각은 ...

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.