我正在使用Entity Framework将数据绑定到GridView。然后,如果用户需要,我需要能够将其导出为PDF。但是,我遇到了一个问题 - 列标题看起来很好,但是之后的每一行都是空的。我放置了一个断点并发现这是因为GridViewRow Cells的文本是空的。但是,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文本属性是一个空字符串。不只是第一行或第二行(标题),而是所有这些。
非常感谢!
答案 0 :(得分:1)
也许是因为你有asp:单元格内的标签控件而不是文本(来自BoundField)。
尝试查找您的控件,如以下示例中的故障单值:
var myTicketLabel = (Label) rowView.FindControl("ticketLabel");