Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.net C#GridView到html表不工作_C#_Asp.net_Gridview_Itextsharp - Fatal编程技术网

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>";
}