本文介绍了如果我使用模板字段,将Gridview转换为Pdf时,我会变空一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案


这篇关于如果我使用模板字段,将Gridview转换为Pdf时,我会变空一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 18:01