Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 空引用对象未设置为实例_C#_Asp.net - Fatal编程技术网

C# 空引用对象未设置为实例

C# 空引用对象未设置为实例,c#,asp.net,C#,Asp.net,这是按钮的代码,我尝试了不同的方法来打印此网格,但总是出现相同的错误。显然,标题行的第二列中没有单元格。在指定单元格的值之前,请尝试创建该单元格: protected void Btn_impencuesta_Click(object sender, EventArgs e) { string FilePath = MapPath("~/File/MyReport.pdf"); iTextSharp.text.Document pdfDoc = new iTextSharp.

这是按钮的代码,我尝试了不同的方法来打印此网格,但总是出现相同的错误。

显然,标题行的第二列中没有单元格。在指定单元格的值之前,请尝试创建该单元格:

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

我尝试过,但如果(gvTotal.HeaderRow.Cells[1]==null)发送了相同的错误//现在错误在这里{gvTotal.AllowPaging=false;gvTotal.HeaderRow.Cells[1]。Text=“Message”;gvTotal.HeaderRow.Font.Bold=true;gvTotal.RenderControl(hw);}
if (gvTotal.HeaderRow.Cells[1] == null)
    gvTotal.HeaderRow.Cells[1] = new TableCell();
gvTotal.HeaderRow.Cells[1].Text = "Message";