Reputation: 13
I have generated a report with table using itext sharp. I want to check the table row is grater than one or not. My table creation is
Document doc = new Document(new Rectangle(570f, 600f));
PdfPTable table = new PdfPTable(2);
table.SetTotalWidth(new float[] { 45f,45f}
cell = new PdfPCell(new Phrase("Name", font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Age", font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Purushu", font2));
table.AddCell(cell);
cell = new PdfPCell(new Phrase("35", font2));
table.AddCell(cell);
doc.Add(table);
I have added table like this way and tried to get table roe count like
if(table.row.Count>0)
{
string data="1"
}
else
{
string data="2"
}
But if the table added or not the row count shows always '0'
. Kindly let me know the reason ? remeber it is itext sharp pdf table.
Upvotes: 1
Views: 4636
Reputation: 77528
You can get the row count like this:
table.Size
However, when I look at the table you create in the code snippet in your question, I see a table with zero completed rows. You create a table with 10 columns, but you are only adding 4 cells.
Moreover, your code should throw an exception because of this:
PdfPTable table = new PdfPTable(10);
table.SetTotalWidth(new float[] { 45f,45f}
You create a table with 10 columns, but define the widths of the columns as if there are only 2 columns. Either you are using a rogue version of iTextSharp (which version are you using?), or your code sample isn't your actual code sample, or your code should throw an error saying something like: you define the widths for 2 columns, your table has 10 columns.
You ask me: the row count shows always '0'. Kindly let me know the reason?
I think the main reason is your inaccuracy when writing code.
Upvotes: 1