Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this code to show data in datagridview


C#
private void button2_Click(object sender, EventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection("Data Source="
                                    + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "Database1.sdf"));

            con.Open();
            SqlCeCommand com = con.CreateCommand();
            com.CommandText = ("select * from client");
            SqlCeDataAdapter dad = new SqlCeDataAdapter(com);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {

                dataGridView2.DataSource = ds.Tables[0];

            }
        }

this in export button

C#
//Creating iTextSharp Table from the DataTable data

            PdfPTable pdfTable = new PdfPTable(dataGridView2.ColumnCount);
            pdfTable.DefaultCell.Padding =2;
            pdfTable.WidthPercentage = 30;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;

            //Adding Header row
            foreach (DataGridViewColumn column in dataGridView2.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
                pdfTable.AddCell(cell);
            }

            //Adding DataRow
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    pdfTable.AddCell(cell.Value.ToString());
                }
            }

            //Exporting to PDF
            string folderPath = "C:\\PDFs\\";
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
            {
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                pdfDoc.Add(pdfTable);
                pdfDoc.Close();
                stream.Close();
            }

this is the error
object reference not set to an instance of an object
"pdftable.AddCell(Cell.Value.ToString());"
Posted
Updated 24-May-15 11:09am
v2

Debug and check what is the null object in the runtime, according to exception occured line, I think cell.Value may have null in your DataGridView. You can add null check as below

C#
foreach (DataGridViewCell cell in row.Cells)
{
   if(cell.Value!=null)
   {
      pdfTable.AddCell(cell.Value.ToString());
   }else
   {
      pdfTable.AddCell("");
   }
}
 
Share this answer
 
thanks alot it's work
C#
foreach (DataGridViewCell cell in row.Cells)
{
   if(cell.Value!=null)
   {
      pdfTable.AddCell(cell.Value.ToString());
   }else
   {
      pdfTable.AddCell("");
   }
}


but it didn't show any arabic value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900