I have a problem while trying to get values of specific columns in dataGridView and add them to the PdtPtable. But the values added in the table is repeated, for example row one is added twice.I tried to go through each row and in every row through each column.
PdfPTable pdfTable= new PdfPTable(5);
foreach(DataGridViewRow row in dataGridView1.Rows) {
foreach (DataGridViewCell celli in row.Cells) {
try {
pdfTable.AddCell(celli.Value.ToString());
}
catch { }
}
doc.Add(pdfTable);
}
I have fixed the indentation in your code snippet. You can now see what you're doing wrong in one glimpse.
You have:
This means that you are creating a table, adding it as many times as there are rows, hence the repetition of the rows.
You should have:
Or better yet:
Now you are adding the table only once.