0
protected void Btn_impencuesta_Click(object sender, EventArgs e)
{

    string FilePath = MapPath("~/File/MyReport.pdf");

    iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
    PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gvTotal.AllowPaging = false;


    gvTotal.HeaderRow.Cells[1].Text = "Message";//Here i got the error :(
    gvTotal.HeaderRow.Font.Bold = true;
    gvTotal.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.ContentType = "Application/pdf";
    Response.WriteFile(FilePath);
    Response.End();
 }

this is the code of the button, i tried different ways to print this grid but i got always the same error.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

Apparently you don't have a cell in that second column of the header row. Try to create the cell before assigning its value:

if (gvTotal.HeaderRow.Cells[1] == null)
    gvTotal.HeaderRow.Cells[1] = new TableCell();
gvTotal.HeaderRow.Cells[1].Text = "Message";
yazanpro
  • 4,512
  • 6
  • 44
  • 66
  • i tried but send me the same error if (gvTotal.HeaderRow.Cells[1] == null) // now the error is here { gvTotal.AllowPaging = false; gvTotal.HeaderRow.Cells[1].Text = "Message"; gvTotal.HeaderRow.Font.Bold = true; gvTotal.RenderControl(hw); } – NoeliaDota2 Jun 21 '15 at 04:19