2017-03-06 1 views
0

내가 DataGridView에 데이터를 내보낼 객체에 인스턴스로 설정되지 않았습니다iTextSharp 라이브러리와 PDF 파일합니다.iTextSharp 오류 개체 참조가

그러나이 오류는 을 클릭하면 발생합니다. PDF 파일을 내 보냅니다.

개체 참조가 개체에 대한 인스턴스로 설정되지 않았습니다.

라인 오류 :이 문제가

//fetch the header text 
cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text); 

만 변수 rowCountProducts가 0과 같다.

아무도 도와 줄 수 있습니까?

미리 감사드립니다. 코드는 아래와 같습니다.

if (rowCountProducts >= 0) 
{ 
    //link button column is excluded from the list 
    int colCount = gvProducts.Columns.Count - 1; 

    //Create a table 
    table = new PdfPTable(colCount); 
    table.HorizontalAlignment = 1; 
    table.WidthPercentage = 100; 

    //create an array to store column widths 
    int[] colWidths = new int[gvProducts.Columns.Count]; 

    PdfPCell cell; 
    string cellText; 

     //create the header row 
     for (int colIndex = 0; colIndex < colCount; colIndex++) 
     { 
      //set the column width 
      table.SetWidths(new int[] { 0, 15, 15, 20, 20, 15, 8, 12, 20, 20, 10, 12, 10, 20, 12, 10, 10, 12, 30, 8, 12, 12, 10 }); 


      //fetch the header text 
      cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text); 

      //create a new cell with header text 
      BaseFont bf = BaseFont.CreateFont(
            BaseFont.HELVETICA, 
            BaseFont.CP1252, 
            BaseFont.EMBEDDED, 
            false); 
      iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE); 
      cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font)); 
      cell.HorizontalAlignment = Element.ALIGN_CENTER; 
      cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
      cell.FixedHeight = 55f; 

      //set the background color for the header cell 
      cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a")); 

      //add the cell to the table. we dont need to create a row and add cells to the row 
      //since we set the column count of the table to 4, it will automatically create row for 
      //every 4 cells 
      table.AddCell(cell); 
     } 

    //export rows from GridView to table 
    for (int rowIndex = 0; rowIndex < gvProducts.Rows.Count; rowIndex++) 
    { 
     if (gvProducts.Rows[rowIndex].RowType == DataControlRowType.DataRow) 
     { 
      for (int j = 0; j < gvProducts.Columns.Count - 1; j++) 
      { 
       //fetch the column value of the current row 
       cellText = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[j].Text); 

       //create a new cell with column value 
       cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8))); 
       cell.HorizontalAlignment = Element.ALIGN_CENTER; 
       cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
       cell.FixedHeight = 55f; 

       string cellText2 = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[6].Text); 

       if (cellText2.ToString() == "B") 
       { 
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5dc")); 
       } 
       if (cellText2.ToString() == "L") 
       { 
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#dcf5f5")); 
       } 
       if (cellText2.ToString() == "T") 
       { 
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffe9de")); 
       } 

       //add the cell to the table 
       table.AddCell(cell); 
      } 
     } 
    } 
} 
+0

당신은'cellText = Server.HtmlDecode (gvProducts.HeaderRow.Cells는 [colIndex]는 .text) 말한다. 내가 알 수있는 한, 해당 라인에서 참조 된 iTextSharp 클래스는 없습니다. 따라서 자신의 버그 검색과 다른 세부 사항에 집중해야합니다. iTextSharp에 대한 질문에 집중하는 것은 iTextSharp에 대해 잘 모르는 사람들을 부끄럽게 만드는 훌륭한 방법이지만, Server.HtmlDecode (gvProducts.HeaderRow.Cells [colIndex]. 텍스트)'. – mkl

답변

0

rowCountProducts

가 0 일 때 문제가있는 경우이를 시도하십시오.

이 도움이 되었기를 바랍니다. '에러 라인이다

//fetch the header text 
//start 
if (rowCountProducts > 0) 
{ 
    cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text); 
} 
else 
{ 
    cellText = ""; 
} 
//end 
관련 문제