简体   繁体   English

ASP.net C#GridView使html表无法正常工作

[英]ASP.net C# GridView to html table not working

So I have this on my mark up: 所以我在标记上有这个:

<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>

and here on my .cs class: 在我的.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;
    }

Which I found here on stackoverflow,The header works fine, but the rows are returning null. 我在stackoverflow上找到的,标题工作正常,但行返回null。 Im using this to print the gridview to pdf using ITextSharp. 我使用它来使用ITextSharp将gridview打印为pdf。

This method also works with other gridview. 此方法也适用于其他gridview。 I dont know why it returns null on this specific gv. 我不知道为什么它在这个特定的gv上返回null。

For a TemplateField with a Label, the value is not in the cell text but in the Label control (which is the second control of the cell, after a Literal control): 对于带有Label的TemplateField,该值不在单元格文本中,而是在Label控件中(在Literal控件之后是单元格的第二个控件):

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

By the way, I kept the string concatenation technique as shown in your code but it would be preferable to use a StringBuilder . 顺便说一句,我保留了代码中显示的字符串连接技术,但最好使用StringBuilder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM