【问题标题】:How to change gridview headings before exporting to pdf (Asp.Net VB)如何在导出为 pdf 之前更改 gridview 标题(Asp.Net VB)
【发布时间】:2015-01-19 11:19:55
【问题描述】:

希望你能帮上忙。我创建了一个函数,它接受一个数据集,将它绑定到一个临时网格视图,然后将其导出为 pdf。

在导出为 pdf 之前,我需要以某种方式更改 gridview 上的标题,以便它们对用户更友好。我添加了一个 RowDataBound 事件,然后检查行类型是否为 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

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

【问题讨论】:

    标签: asp.net pdf gridview


    【解决方案1】:

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

    AddHandler tmpGridView.RowDataBound, AddressOf tmpGridView_RowDataBound
    

    上面这行代码需要在前面加上

    tmpGridView.DataBind()
    

    否则事件不会被触发。现在排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      相关资源
      最近更新 更多