如何在导出为pdf之前更改gridview标题(Asp.Net VB)

时间:2015-01-19 11:19:56

标签: asp.net pdf gridview

我希望你能提供帮助。我创建了一个获取数据集的函数,将其绑定到临时gridview,然后将其导出为pdf。

我需要在导出到pdf之前以某种方式更改gridview上的标题,以便它们更加用户友好。我添加了一个RowDataBound事件,然后检查了rowtype是否为DataControlRowType.Header并尝试以这种方式修改它,但我仍然看到数据集中未更改的版本。到目前为止,这是我的代码:

Dim context As HttpContext = HttpContext.Current
        Dim response As HttpResponse = HttpContext.Current.Response
        Dim tmpGridView As New GridView()
        tmpGridView.BorderWidth = 0
        tmpGridView.ControlStyle.Font.Size = 8
        tmpGridView.AllowPaging = False
        tmpGridView.DataSource = ds
        tmpGridView.DataBind()
        AddHandler tmpGridView.RowDataBound, AddressOf tmpGridView_RowDataBound
        response.ContentType = "application/pdf"
        response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")
        response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        tmpGridView.RenderControl(hw)
        Dim sr As New StringReader(sw.ToString())
        Dim pdfDoc As New Document()
        pdfDoc.SetMargins(20.0F, 20.0F, 20.0F, 20.0F)
        If Rotation = "Vertical" Then
            pdfDoc.SetPageSize(PageSize.A4)
        ElseIf Rotation = "Horizontal" Then
            pdfDoc.SetPageSize(PageSize.A4.Rotate())
        End If
        Dim Image As String = context.Server.MapPath("~/Images/head_logo.gif")
        Dim headerImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Image)
        headerImage.ScalePercent(100.0F)
        headerImage.Alignment = Element.ALIGN_LEFT
        headerImage.SpacingAfter = 50.0F
        Dim HeadingFont As iTextSharp.text.Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)
        Dim ParagraphFont As iTextSharp.text.Font = FontFactory.GetFont(FontFactory.HELVETICA, 8)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, response.OutputStream)
        pdfDoc.Open()
        Dim cb As PdfContentByte = writer.DirectContent
        pdfDoc.Add(headerImage)
        Dim ct As New ColumnText(cb)
        Dim heading As New Phrase(ReportName, HeadingFont)
        ct.SetSimpleColumn(heading, 480, 780, 50, 50, 10, Element.ALIGN_RIGHT)
        ct.Go()
        pdfDoc.Add(New Paragraph("Generated on " & Date.Now.ToString("dd MMMM yyyy"), ParagraphFont))
        pdfDoc.Add(New Paragraph(vbCrLf))
        htmlparser.Parse(sr)
        pdfDoc.Close()
        response.Write(pdfDoc)
        response.End()

这是我的tmpGridView_RowDataBound事件中的代码

If e.Row.RowType = DataControlRowType.Header Then
        Dim Heading1Value As String = e.Row.Cells(0).Text
        Select Case Heading1Value
            Case "SaleID"
                e.Row.Cells(0).Text = "ID"
        End Select
    End If

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

原来我是以错误的顺序执行代码。

AddHandler tmpGridView.RowDataBound, AddressOf tmpGridView_RowDataBound

上面的代码行需要在

之前添加
tmpGridView.DataBind()

否则该事件未被解雇。现在排序。