Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hi, I am facing issue empty on templateField when i convert gridview to pdf.

I am binding grid view from databae using sqlcommand.

cmd.CommandText = "select orderno, shname,totalnum2,orderType from tabelname where reforderno is not null";
GridView1.DataSource = ds;
GridView1.DataBind();


aspx page:


<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" >
<Columns>

<asp:TemplateField HeaderText="Order#" HeaderStyle-Font-Bold="false" HeaderStyle-Width="57">
<ItemTemplate>
<asp:LinkButton ID="lnkallorder" ForeColor="#4b4b4b" runat="server" OnClientClick="aspnetForm.target ='_blank';" Text='<%# DataBinder.Eval(Container.DataItem,"orderno") %>' CommandName="all" OnClick="orderno_clicked"></asp:LinkButton>
</ItemTemplate><ItemStyle Width="57" />
</asp:TemplateField>

<asp:BoundField ItemStyle-Width = "200px" DataField = "shname" HeaderText = "CustomerID" />
<asp:BoundField ItemStyle-Width = "100px" DataField = "totalnum2" HeaderText = "City"/>
<asp:BoundField ItemStyle-Width = "50px" DataField = "orderType" HeaderText = "Country"/>
</Columns>
</asp:GridView>

<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="btnExportPDF_Click" />

Now i have output follow

Order#  CustomerID       City  Country
111         aaaa1          2        Y
222         bbbb2          3        Y
333         cccc3          4        N

ExportToPDF // this is button click event which generates gridview to pdf.

coding for gridview to pdf
aspx.cs page
protected void btnExportPDF_Click(object sender, EventArgs e)
{

//Create a table
iTextSharp.text.Table table = new iTextSharp.text.Table(GridView1.Columns.Count);
table.Cellpadding = 5;

//Set the column widths
int[] widths = new int[GridView1.Columns.Count];
for (int x = 0; x < GridView1.Columns.Count; x++)
{
widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value ;
string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
cell.BackgroundColor = new Color (System.Drawing.ColorTranslator.FromHtml("#008000"));
table.AddCell(cell);
}
table.SetWidths(widths);

//Transfer rows from GridView to table
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);

//Set Color of Alternating row
if (i % 2 != 0)
{
cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B"));
}
table.AddCell(cell);
}
}
}

//Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}

But now i am getting result in pdf follow

Order#      CustomerID        City   Country
                aaaa1           2     Y
                bbbb2           3     Y
                cccc3           4     N

The Order# is getting "" empty as i used template field. if i use databound i am getting all column without empty. But i need show link on gridview for orderno.

Why i am getting empty if i use tempate field. how to solve this problem.

Help needed.

Thanks
Posted

1 solution

try use LinkButton.Text , not the cell[0].Text
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900