Click here to Skip to main content
15,889,849 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code m using datagridview
public void pdf()
{

    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));

    doc.Open();//Open Document to write


    Paragraph paragraph = new Paragraph("data Exported From DataGridview!");

    // Create table by setting table value

    Table t1 = new Table(2);
    DataTable dt = (DataTable)datagrigview.DataSource;

    // Create Table Header

    Cell cid = new Cell("Tank Name");
    Cell cname = new Cell("Frames");

    t1.AddCell(cid);
    t1.AddCell(cname);

    foreach (DataGridViewRow rows in dgv.Rows)
    {

        string id = dgv.Rows[rows.Index].Cells["Tank_Name"].Value.ToString();  //I Got Error here that Cells["Tank_Name"] is object refrance is not set as an instance of an object
        string name = dgv.Rows[rows.Index].Cells["Frames"].Value.ToString();
        //  Create Cells
        Cell c2 = new Cell(id);
        Cell c1 = new Cell(name);
        //  Adding cells
        t1.AddCell(c1);
        t1.AddCell(c2);

    }
    doc.Add(paragraph);
    doc.Add(t1);
    doc.Close(); //Close document

    MessageBox.Show("PDF Created!");

}
Posted
Comments
[no name] 22-Jul-14 7:42am    
In the future do not use comments in your code to ask question or describe a problem. It's really hard to see.

Of course you will. You have trying to get the value from a cell that does not exist. Since you have not populated the grid with any data this should not be a surprise to you.

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