【问题标题】:Converting GridView to DataTable - Cell Text Empty将 GridView 转换为 DataTable - 单元格文本为空
【发布时间】:2023-03-12 05:44:01
【问题描述】:

我正在使用实体框架将数据绑定到 GridView。然后,如果用户需要,我需要能够将其导出为 PDF。但是,我遇到了一个问题 - 列标题看起来很好,但之后的每一行都是空的。我放置了一个断点,发现这是因为 GridViewRow 单元格的文本是空的。但是,GridView 中显然有数据。为什么会发生这种情况?

aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="all.aspx.cs" Inherits="Ticket_System.reports.all" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
     <asp:GridView ID="allTicketGrid" AutoGenerateColumns="false" runat="server" SortedDescendingHeaderStyle-VerticalAlign="NotSet" Enabled="False">
        <Columns>
            <asp:TemplateField HeaderText="Ticket">
                <ItemTemplate>
              <asp:Label ID="ticketLabel" runat="server" Text='<%# Bind("TICKET_ID") + Bind("STATUS_TYPE.DESCRIPTION") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                               <asp:TemplateField HeaderText="Property">
                                                   <ItemTemplate >
                <asp:label runat="server" id="propLabel" text='<%# Bind("propName") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>
                   <asp:TemplateField HeaderText="Item Description">
                <ItemTemplate>
                <asp:label runat="server" id="itemDescriptionLabel" text='<%# Bind("itemViewField") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>
                        <asp:TemplateField HeaderText="Opening Notes">
                <ItemTemplate >
                <asp:label runat="server" id="propLabel" text='<%# Bind("OPEN_STATUS.NOTES") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>     
                           <asp:TemplateField HeaderText="Opened On">
                <ItemTemplate >
                <asp:label runat="server" id="openDateLabel" text='<%# Bind("OPEN_STATUS.UPDATED_DATE") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>   
                            <asp:TemplateField HeaderText="Opened By">
                <ItemTemplate >
                <asp:label runat="server" id="openUserLabel" text='<%# Bind("OPEN_STATUS.endUser") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>                 
               <asp:TemplateField HeaderText="Last Updated">
                <ItemTemplate  >
                <asp:label runat="server" id="lastUpdateLabel" text='<%# Bind("LATEST_STATUS.UPDATED_DATE") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>
                 <asp:TemplateField HeaderText="Last Updated By">
                <ItemTemplate >
                <asp:label runat="server" id="lastUserLabel" text='<%# Bind("LATEST_STATUS.END_USER.FIRST_NAME") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>
                   <asp:TemplateField HeaderText="Latest Notes">
                <ItemTemplate >
                <asp:label runat="server" id="lastCommentLabel" text='<%# Bind("LATEST_STATUS.NOTES") %>'></asp:label></label>
                    </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>
    <asp:Button Text="Export to PDF" ID="exportReportButton" OnClick="exportReport" runat="server" />
</asp:Content>

和cs

 public partial class all : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                using (ticketModel dbContext = new ticketModel())
                {
                    allTicketGrid.DataSource = dbContext.TICKETs.ToList();
                    allTicketGrid.DataBind();
                }
            }
        }

        protected void exportReport(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

          for (int i = 0; i < allTicketGrid.Columns.Count; i++)
            {
                dt.Columns.Add(allTicketGrid.Columns[i].HeaderText);
            }
            foreach (GridViewRow rowView in allTicketGrid.Rows)
            {
                DataRow dr = dt.NewRow();

               for (int i = 0; i < rowView.Cells.Count; i++)
                {

                    dr[i] = rowView.Cells[i].Text;
                }
               dt.Rows.Add(dr);

            } 

               Document doc = pdfWorker.readyDocument();
            pdfWorker.createTable(doc, dt);
            pdfWorker.savePDF(doc, "C:/Users/Khandokar/Documents/Test.pdf");
            }

        }

        }

问题在于这一行dr[i] = rowView.Cells[i].Text; rowView.Cells 文本属性是一个空字符串。不仅是第一行或两行(标题),而是全部。

非常感谢!

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    可能是因为您在单元格中有 asp:Label 控件而不是文本(来自 BoundField)。

    尝试在以下示例中找到您的控件以获取门票价值:

    var myTicketLabel = (Label) rowView.FindControl("ticketLabel");

    【讨论】:

    • 看起来你一针见血。谢谢!我的解决方案是使用 Bound Fields 而不是 TemplateFields。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2013-01-28
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多