Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 197.9k

Itext sharp print value from datagrid null c#

Dec 20 2018 3:59 AM
Some help pelease
 
Error line 48
 
If is value null error 
 
{"Object reference not set to an instance of an object."} 
 
System.NullReferenceException
 
System.Windows. Forms.DataGridViewCelll.Value.get returned null
 
Some help?
 
  1.  private void pdfButton_Click(object sender, EventArgs e)  
  2.         {  
  3.             //Creating iTextSharp Table from the DataTable data  
  4.             PdfPTable pdfTable = new PdfPTable(prijava_radnikaDataGridView.ColumnCount -1);  
  5.             pdfTable.DefaultCell.Padding = 3;  
  6.             pdfTable.WidthPercentage = 100;  
  7.             pdfTable.HorizontalAlignment = Element.ALIGN_CENTER;  
  8.             pdfTable.DefaultCell.BorderWidth = 1;  
  9.             //float[] widths = new float[] { 0f, 18f, 40f, 35f, 35f, 25f, 25f, 18f, 40f, 35f };  
  10.             //pdfTable.SetWidths(widths);  
  11.             BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  12.             iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.WHITE);  
  13.             iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 9);  
  14.   
  15.   
  16.             //Adding Header row  
  17.             foreach (DataGridViewColumn column in prijava_radnikaDataGridView.Columns)  
  18.             {  
  19.   
  20.   
  21.                 if (column.Index == 1)  
  22.                 {  
  23.                 }  
  24.                 else  
  25.                 {  
  26.                     PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri));  
  27.                     cell.BackgroundColor = new BaseColor(89, 135, 178);  
  28.                     cell.VerticalAlignment = Element.ALIGN_MIDDLE;  
  29.                     pdfTable.AddCell(cell);  
  30.                 }  
  31.             }  
  32.   
  33.              
  34.   
  35.             //Adding DataRow  
  36.             foreach (DataGridViewRow row in prijava_radnikaDataGridView.Rows)  
  37.             {  
  38.                   
  39.                 foreach (DataGridViewCell cell in row.Cells)  
  40.                 {  
  41.                     if (cell.ColumnIndex == 1 )  
  42.                     {  
  43.                         PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));  
  44.                         pdfTable.AddCell(cell2);  
  45.                     }  
  46.                     else  
  47.                     {  
  48.                         PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
  49.                         pdfTable.AddCell(cell2);  
  50.                     }  
  51.                       
  52.                 }  
  53.             }  
  54. etc.....code....<<  

 

Answers (2)