KellyM
KellyM

Reputation: 2522

Converting GridView to DataTable - Cell Text Empty

I am using Entity Framework to bind data to a GridView. I then need to be able to export this to a PDF if the user requires. However, I have encountered a problem - the column headers appear just fine, but every row after that is empty. I placed a breakpoint and discovered that this because the text of the GridViewRow Cells are empty. However, there clearly is data in the GridView. Why might this be happening?

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

and the 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");
            }

        }

        }

The problem is at this line dr[i] = rowView.Cells[i].Text; the rowView.Cells text property is an empty string. Not just the first row or two (the header), but all of them.

Thanks so much!

Upvotes: 0

Views: 430

Answers (1)

Michel Amorosa
Michel Amorosa

Reputation: 495

Maybe it's because you have asp:Label controls within cells and not a text (from a BoundField).

Try to find your controls like in the following example for ticket value:

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

Upvotes: 1

Related Questions