ASP.net C#GridView到html表不工作
所以我在我的加价上有这个:ASP.net C#GridView到html表不工作,c#,asp.net,gridview,itextsharp,C#,Asp.net,Gridview,Itextsharp,所以我在我的加价上有这个: <asp:GridView ID="GridView1" runat="server" ItemType="IR_InfantRecord.Models.Immunization" DataKeyNames="ImmunizationNumber" SelectMethod="patientImmunGrid_GetData" AutoGenerateColumns="False" ... <Columns
<asp:GridView ID="GridView1" runat="server" ItemType="IR_InfantRecord.Models.Immunization" DataKeyNames="ImmunizationNumber" SelectMethod="patientImmunGrid_GetData" AutoGenerateColumns="False"
...
<Columns>
<asp:TemplateField HeaderText="EmpName">
<ItemTemplate>
<asp:Label Text="<%# Item.Emp.EmpName%>"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:Label Text="<%# Item.Emp.Check%>"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:DynamicField DataField="Status" />
<asp:DynamicField DataField="DateTaken" />
...
</asp:GridView>
...
在我的.cs课上:
public static string ConvertGVToHTML(GridView gv)
{
string html = "<table>";
//add header row
html += "<tr>";
for (int i = 0; i < gv.Columns.Count; i++)
html += "<td>" + gv.Columns[i].HeaderText + "</td>";
html += "</tr>";
//add rows
for (int i = 0; i < gv.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < gv.Columns.Count; j++)
html += "<td>" + gv.Rows[i].Cells[j].Text.ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html;
}
公共静态字符串ConvertGVToHTML(GridView gv)
{
字符串html=“”;
//添加标题行
html+=“”;
对于(int i=0;i
我在stackoverflow上发现,标题工作正常,但行返回null。我用这个来打印gridview到pdf使用ITextSharp
此方法也适用于其他gridview。我不知道为什么它在这个特定gv上返回null。对于带有标签的TemplateField,该值不在单元格文本中,而是在Label控件中(它是单元格的第二个控件,位于文字控件之后):
for(int i=0;i1&&cell.Controls[1]为标签)
{
Label lblValue=单元格。控件[1]作为标签;
html+=lblValue.Text;
}
其他的
{
html+=cell.Text;
}
html+=“”;
}
html+=“”;
}
顺便说一句,我保留了代码中显示的字符串连接技术,但是最好使用
StringBuilder
您确定gridview中有行吗?不客气!抱歉,我刚刚对tr
标记进行了更正,然后才注意到您的编辑“挂起”(第一次有人编辑我的答案之一)。没关系!再次感谢你!!
for (int i = 0; i < gv.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < gv.Columns.Count; j++)
{
TableCell cell = gv.Rows[i].Cells[j];
html += "<td>";
if (cell.Controls.Count > 1 && cell.Controls[1] is Label)
{
Label lblValue = cell.Controls[1] as Label;
html += lblValue.Text;
}
else
{
html += cell.Text;
}
html += "</td>";
}
html += "</tr>";
}