null引用对象未设置为实例

时间:2015-06-21 03:50:16

标签: c# asp.net

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();
 }

这是按钮的代码,我尝试了不同的方法来打印这个网格,但我总是遇到同样的错误。

1 个答案:

答案 0 :(得分:0)

显然,您在标题行的第二列中没有单元格。尝试在分配其值之前创建单元格:

if (gvTotal.HeaderRow.Cells[1] == null)
    gvTotal.HeaderRow.Cells[1] = new TableCell();
gvTotal.HeaderRow.Cells[1].Text = "Message";