You must Sign In to post a response.
  • Category: Silverlight

    How to export a datagrid values in PDF

    Hi,

    I would like to export the records form datagrid to a PDF file.googled for many samples, blogs but no help. Can anyone help me with that.

    Thanks in advance,
    nitz.
  • #713686
    Best Example .

    http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-pdf.html

    Regards
    Rakesh Chaubey
    rakeshchaubey1989@gmail.com

  • #713703
    Hi,
    Please find the following code for achieving this.



    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    String strCon = System.Configuration.ConfigurationManager
    .ConnectionStrings["EmpDBConnectionSring"].ConnectionString;
    SqlConnection con = new SqlConnection(strCon);
    con.Open();

    string strSQL = "SELECT * FROM Emptab";
    SqlConnection con = new SqlConnection(strCon);
    SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "test");

    GvEmp.DataSource = ds.Tables[0];
    GvEmp.DataBind();
    }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=EmployeeDetails.pdf");
    Response.Charset = "";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);
    GvEmp.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();

    }




    Thanks
    Padma

    Have a good Day

  • #713711
    Thanks Padma.

    I forgot to mention that I need it for my silverlight application.

    When I googled I got it like for silverlight we need third party tools. But for my application I cannot use any third party controls. So if anyone have a working code please share it with me.

  • #713715
    you can try thi blow link to

    http://www.dotnetspider.com/resources/29759-Exporting-GridView-PDF.aspx

    I Hope To Use Full..
    And Choice The Best Answers

    php-wordpress Developer
    you can any problem to php wordpress to contact me
    jivanibhai7306@gmail.com
    Thanks!!...

  • #713785
    This Application is used to convert Gridview to PDF document
    using itext itextsharp(Its a free tool).
    Download the itextsharp from link given below and add this dll in the bin directory.
    http://sourceforge.net/projects/itextsharp/
    or
    http://nodevice.in/dll/itextsharp_dll/item21198.html
    After placing the dll in the bin directory write the below code in the aspx.vb page.

    In your code-behind, try just adding

    Imports iTextSharp.text.pdf
    Imports iTextSharp.text

    Here with i had attached the sample project.



    Imports iTextSharp.text.pdf
    Imports iTextSharp.text
    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class _Default
    Inherits System.Web.UI.Page
    Dim ds As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
    ' ShowGridview()
    End If
    End Sub

    Sub ShowGridview()
    'bind the database values to Gridveiw using dataset control
    Dim con As SqlClient.SqlConnection
    Dim da As SqlDataAdapter

    Dim str As String
    str = "USER=UserID;PASSWORD=PASSWORD;SERVER=SERVER-Name;DATABASE=DATABASE-Name"
    con = New SqlConnection(str)
    da = New SqlDataAdapter("select FieldName1,FieldName2 from Table_Name", con)
    da.Fill(ds, "Employee")
    ViewState("DataSetValue") = ds
    grvGrid.DataSource = ds
    grvGrid.DataBind()
    con.Close()

    End Sub
    Private Sub ConvertGridviewToPdfDocument(ByVal dstHeader2 As DataSet)
    Dim doc As Document = New Document
    PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\3.pdf", FileMode.Create))
    doc.Open()
    Dim table As New PdfPTable(dstHeader2.Tables(0).Columns.Count)
    Dim widths As Integer() = {25, 25}
    table.WidthPercentage = "100"
    For i As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
    Dim ColumnHeader As New Paragraph(dstHeader2.Tables(0).Columns(i).ColumnName.ToString(), FontFactory.GetFont("verdana", 11))
    ColumnHeader.Font.SetStyle(Font.BOLD)
    Dim cell As New PdfPCell(New Phrase(ColumnHeader))
    cell.HorizontalAlignment = Element.ALIGN_CENTER
    table.AddCell(cell)
    Next

    For k As Integer = 0 To dstHeader2.Tables(0).Rows.Count - 1
    For j As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
    Dim ColumnValue As New Paragraph(dstHeader2.Tables(0).Rows(k)(j).ToString(), FontFactory.GetFont("verdana", 10))
    table.AddCell(ColumnValue)
    Next
    Next
    table.SetWidths(widths)
    doc.Add(table)
    doc.Add(New Paragraph(vbLf))
    doc.Close()
    Response.Redirect("~/3.pdf")
    End Sub

    Protected Sub btnConvertGridviewToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertGridviewToPDF.Click
    'ConvertGridviewToPdfDocument(ViewState("DataSetValue"))
    Dim str As String = ""
    Dim doc As Document = New Document
    PdfWriter.GetInstance(doc, New FileStream(FileUpload1.PostedFile.FileName, FileMode.Create))
    'string strFileNameWithPath = FileUpload1.PostedFile.FileName;
    doc.Open()
    doc.Close()

    End Sub
    End Class




    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.