Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Row and column are not in correct way when pdf file generated .In delete column 'id' come and in id column " 'Fname' come" and so on.Here is my PDF file http://s8.postimg.org/nit4hghbp/Untitled.jpg[^]
Here is my datagridview image
http://s4.postimg.org/72aj4aar1/Untitled.jpg[^]

I want that first two columns data update and delete data print in pdf file and remaining column data printed.
Here is my code,Please help me to resolve this error.
C#
private void button6_Click(object sender, EventArgs e)
        {
            //Creating iTextSharp Table from the DataTable data
            PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
            
            pdfTable.DefaultCell.Padding = 3;
            
            pdfTable.WidthPercentage = 70;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;
            //Adding Header row
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
        cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
        pdfTable.AddCell(cell);
    }
 
    //Adding DataRow
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            if (cell.Value == null)
            {
                
            }
            else
            {
                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();
    }

        }
        } 
    }
Posted

1 solution

The problem is that you have records where the bitmap is not assigned, so in this part of the code:

C#
if (cell.Value == null)
{
     // if bitmap is null then the cell does not get output, this causes the cells to slide
}
else
{
    pdfTable.AddCell(cell.Value.ToString());

}


Not sure on the correct code but you should do something like

C#
if (cell.Value == null) && (cell.ValueType == string)
 
Share this answer
 
v2
Comments
Hameed Khan 3-Jan-16 3:03am    
@Beefcake500 Thanks for response,but your provided code is not working :( :( error come at cell.ValueType==string ,error come 'invalid expression term string'
Matt Comb 3-Jan-16 6:44am    
It wasn't working code sorry as I don't have your code, it was just to help you in the right direction. The problem is because when bitmap is null (for the columns with images) its skipping the output of that column, it needs to output the cell in this case.

here just do this:

if (cell.Value == null)
{
pdfTable.AddCell("");

}
else
{
pdfTable.AddCell(cell.Value.ToString());

}
Hameed Khan 3-Jan-16 8:30am    
@Beefcake Thanksssssssssssssssssss It's working now,here is output

http://s12.postimg.org/agcf443al/Untitled.jpg,

but here is one problem,i want that first two columns update and delete not print in pdf file,then how can i do this???

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