Foros del Web » Programando para Internet » ASPX (.net) »

Exportar Fila seleccionada de GridView a PDF

Estas en el tema de Exportar Fila seleccionada de GridView a PDF en el foro de ASPX (.net) en Foros del Web. Buenos días con todos espero por favor puedan ayudarme con este tema les explico un poco, tengo un GridView en el cual al seleccionar las ...
  #1 (permalink)  
Antiguo 21/12/2013, 11:05
 
Fecha de Ingreso: agosto-2013
Mensajes: 3
Antigüedad: 11 años, 2 meses
Puntos: 0
Mensaje Exportar Fila seleccionada de GridView a PDF

Buenos días con todos espero por favor puedan ayudarme con este tema les explico un poco, tengo un GridView en el cual al seleccionar las filas con el CheckBox me exporta a pdf los seleccionados, lo que deseo hacer es que no me exporte todos en un solo documento PDF sino en documentos separados según las filas seleccionadas, de antemano muchas gracias con todos y espero por favor puedan ayudarme. Este es el código que utilizo para exportar a pdf.

Cita:
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Collections
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser

Partial Class ExportAll
Inherits System.Web.UI.Page

Private con As New SqlConnection(ConfigurationManager.ConnectionStrin gs("conString").ToString())

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

Private Sub BindGridData()
Dim da As New SqlDataAdapter("select CategoryID,CategoryName,Description from categories", con)
Dim dt As New DataTable
da.Fill(dt)

If dt.Rows.Count > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
Else
dt.Rows.Add(dt.NewRow())
GridView1.DataSource = dt
GridView1.DataBind()
Dim columnCount As Integer = GridView1.Rows(0).Cells.Count
GridView1.Rows(0).Cells.Clear()
GridView1.Rows(0).Cells.Add(New TableCell())
GridView1.Rows(0).Cells(0).ColumnSpan = columnCount
GridView1.Rows(0).Cells(0).Text = "<font color=Red><b><center>No Data Found !</center></b></font>"
End If
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
'MyBase.VerifyRenderingInServerForm(control)
End Sub

Private Sub SaveCheckedValues()
Dim userdetails As New ArrayList
Dim index As Integer = -1

For Each gvrow As GridViewRow In GridView1.Rows
index = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
Dim result As Boolean = DirectCast(gvrow.FindControl("chkSelect"), CheckBox).Checked

If ViewState("CHECKED_ITEMS") IsNot Nothing Then
userdetails = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not userdetails.Contains(index) Then
userdetails.Add(index)
End If
Else
userdetails.Remove(index)
End If
Next

If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
ViewState("CHECKED_ITEMS") = userdetails
End If
End Sub

Private Sub PopulateCheckedValues()
Dim userdetails As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
For Each gvrow As GridViewRow In GridView1.Rows
Dim index As Integer = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
If userdetails.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
SaveCheckedValues()
GridView1.PageIndex = e.NewPageIndex

End Sub

Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPDF.Click
Try
SaveCheckedValues()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment;filename=StudentDetail’s .pdf"))
Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.Charset = ""
Response.ContentType = "application/pdf"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
BindGridData()
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
GridView1.HeaderRow.Cells(0).Visible = False
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
If ViewState("CHECKED_ITEMS") IsNot Nothing Then
Dim CheckBoxArray As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
Dim rowIdx As Integer = 0
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
row.Visible = False
Dim index As Integer = CInt(GridView1.DataKeys(row.RowIndex).Value)
If CheckBoxArray.Contains(index) Then
row.Visible = True
row.Cells(0).Visible = False
End If
rowIdx += 1
Next
End If
For Each row As GridViewRow In GridView1.Rows
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Dim controls As New List(Of Control)()
For Each control As Control In cell.Controls
controls.Add(control)
Next
For Each control As Control In controls
Select Case (control.[GetType]().Name)
Case "CheckBox"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, CheckBox).Text _
})
cell.Visible = False
Exit Select
End Select
cell.Controls.Remove(control)
Next
Next
Next
GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Dim sr As New StringReader(sw.ToString())
Dim PDF As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 10.0F)
Dim htmlparser As New HTMLWorker(PDF)
PdfWriter.GetInstance(PDF, Response.OutputStream)
PDF.Open()
htmlparser.Parse(sr)
PDF.Close()
Response.Output.Write(sw.ToString())
Response.[End]()
Catch ex As Exception
End Try
End Sub
End Class

Etiquetas: fila, gridview, pdf, text
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:28.