2014-01-12 33 views
0

我目前使用字符串生成器和各種循環來將一些HTML解析爲webBrowser控件。但我怎麼能具體每個專欄和價值從我的dataGridView1,以便我可以包裝HTML周圍。我有一個列dataGridView1中的行值,這是一個圖像的URL。如何使用c爲每個dataGridView行項目指定HTML#

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; 


} 
+0

您的代碼錯位了一些標籤,並錯過了第一次出現時的標籤。無論如何,我不知道這是否與你的問題有關,我不知道這一點。 – elgonzo

+0

簡單來說,我如何顯示我的dataGridView的一行 – PriceCheaperton

+0

我剛剛編輯/更新我的評論。如果問題與行相關,那麼它就像是與標籤的錯誤有關。 – elgonzo

回答

0
//create table body 
strB.AppendLine("<tr>"); 

我認爲這必須是

//create table body 
strB.AppendLine("</tr>"); 

你開始forlooping多個表格行之前關閉該行標籤。

如果你在你的return語句中放置一個斷點,你可以將StrB的內容複製到一個HTML編輯器中,該編輯器應該突出你的錯誤。

相關問題