C# 将gridview导出为PDf时更改标题的颜色

C# 将gridview导出为PDf时更改标题的颜色,c#,asp.net,pdf,gridview,colors,C#,Asp.net,Pdf,Gridview,Colors,我正在将gridview导出为pdf。但是,当生成PDF时,背景为白色,标题字体颜色为灰色,一切正常。真的很难看到。我正在尝试在导出之前将标题前景色更改为黑色。它不起作用了 有什么建议吗 public void ExportToPDF() { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=C

我正在将gridview导出为pdf。但是,当生成PDF时,背景为白色,标题字体颜色为灰色,一切正常。真的很难看到。我正在尝试在导出之前将标题前景色更改为黑色。它不起作用了

有什么建议吗

  public void ExportToPDF()
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=CallDetail.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        this.GetData();
        GridView1.RenderControl(hw);
        HtmlForm frm = new HtmlForm();
        frm.Attributes["runat"] = "server";
        GridView1.HeaderRow.Style.Add("width", "15%");
        GridView1.HeaderRow.Style.Add("font-size", "10px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "26px");
        for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
        {
            TableCell tc = GridView1.HeaderRow.Cells[col];
            tc.Style.Add("color", "#FFFFFF");
            tc.Style.Add("background-color", "#444");
        }
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();


    }
public void exporttopf()
{
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=CallDetail.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
GridView1.AllowPaging=false;
这是GetData();
GridView1.渲染控制(hw);
HtmlForm frm=新的HtmlForm();
frm.Attributes[“runat”]=“server”;
GridView1.HeaderRow.Style.Add(“宽度”、“15%”);
GridView1.HeaderRow.Style.Add(“字体大小”,“10px”);
GridView1.Style.Add(“文本装饰”、“无”);
添加(“字体系列”、“Arial、Helvetica、sans serif;”;
GridView1.Style.Add(“字体大小”,“26px”);
for(int col=0;col


更新***添加了更新的代码和屏幕截图。

在导出前直接设置标题的
前景色
,不会真正影响标题样式。另一种方法是迭代所有标题单元格,并设置为所需的任何样式(这确实有效)。您可以尝试更改下面的代码行

GridView1.HeaderStyle.ForeColor = System.Drawing.Color.Black;

for(int col=0;col
谢谢您的回复。我应用了下面的代码,输出仍然相同。我更新了我的帖子。它现在包括最新的代码和屏幕截图。@DennisR感谢这对我的帮助。我可以问一下您如何更改边框颜色吗?@User6675636b20796f7521您可以添加边框颜色,如
tc.Style.add(“边框颜色”),“#cfdbe2”)或尝试类似的
tc.Style.Add(“border”,“1px solid#333333”)
for (int col = 0; col < GridView1.HeaderRow.Controls.Count; col++)
{
    TableCell tc = GridView1.HeaderRow.Cells[col];
    tc.Style.Add("color", "#FFFFFF");
    tc.Style.Add("background-color", "#444");
}