I have created an example by refering the below article.
Now please take its reference and correct your code as per your logic if required.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="150px" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="100px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
</Columns>
</asp:GridView>
<asp:Button Text="Export" runat="server" OnClick="ExportToPDF" />
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Code
C#
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Fee") });
dt.Rows.Add(1, "John Hammond", DBNull.Value);
dt.Rows.Add(2, "Mudassar Khan", null);
dt.Rows.Add(3, "Suzanne Mathews", "0");
dt.Rows.Add(4, "Robert Schidner", "0");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.AllowPaging = false;
this.BindGrid();
bool isAllNullOrZero = false;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[2].Text == null || row.Cells[2].Text.Trim() == "0" || row.Cells[2].Text.Trim() == " ")
{
isAllNullOrZero = true;
}
else
{
isAllNullOrZero = false;
break;
}
}
if (isAllNullOrZero)
{
GridView1.Columns[2].Visible = false;
}
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
VB.Net
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Fee")})
dt.Rows.Add(1, "John Hammond", DBNull.Value)
dt.Rows.Add(2, "Mudassar Khan", Nothing)
dt.Rows.Add(3, "Suzanne Mathews", "0")
dt.Rows.Add(4, "Robert Schidner", "0")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub ExportToPDF(ByVal sender As Object, ByVal e As EventArgs)
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.AllowPaging = False
Me.BindGrid()
Dim isAllNullOrZero As Boolean = False
For Each row As GridViewRow In GridView1.Rows
If row.Cells(2).Text Is Nothing OrElse row.Cells(2).Text.Trim() = "0" OrElse row.Cells(2).Text.Trim() = " " Then
isAllNullOrZero = True
Else
isAllNullOrZero = False
Exit For
End If
Next
If isAllNullOrZero Then
GridView1.Columns(2).Visible = False
End If
GridView1.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Dim pdfDoc As Document = New Document(PageSize.A2, 10F, 10F, 10F, 0F)
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
End Using
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Screenshot