Reputation: 519
I have a table with fixed cells of 2. I want to make the visibility of table cell hidden as per my condition. My condition is
PdfPTable table1 = new PdfPTable(2);
table1.SetTotalWidth(new float[] { 200f, 100f});
table1.TotalWidth = 800f;//table size
foreach (GridViewRow row in grdtimetable.Rows)
{
Label lblday = (Label)row.FindControl("lbltopicvalue");
Label lblperiod1 = (Label)row.FindControl("lblperiod1");
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Days", time550));
if(lblperiod1 .Text!="NULL"||lblperiod1 .Text!="")
{
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase(""+lblperiod1 .text+"", time550));
}
else
{
//Here I want to make this cell visibility hidden
}
}
Is it possible? If it is, kindly help me
Upvotes: 0
Views: 2869
Reputation: 4871
There are several ways to do this. I'll illustrate a few.
If you want to keep the positions of the other table cells the same, i.e. not shift them to the left, you can simple add empty cells:
doc.Add(new Paragraph("Option 1:"));
PdfPTable table = new PdfPTable(5);
table.SpacingBefore = 10;
for (int i = 1; i <= 5; i++)
{
if (i == 2 || i == 3)
{
// Add an empty cell
PdfPCell empty = new PdfPCell();
empty.Border = PdfPCell.NO_BORDER;
table.AddCell(empty);
}
else
{
table.AddCell(new PdfPCell(new Phrase("Cell " + i)));
}
}
doc.Add(table);
If you do want to shift cells to the left, but preserve column width, you can add empty cells at the end of the row. This is useful if you want have the same table layout for multiple tables with a different number of empty cells.
(You can get the same result by adjusting the table width, instead of adding empty cells, but it would involve more calculations.)
doc.Add(new Paragraph("Option 2:"));
PdfPTable table2 = new PdfPTable(5);
table2.SpacingBefore = 10;
int emptycells = 0;
for (int i = 1; i <= 5; i++)
{
if (i == 2 || i == 3)
{
// Count the number of empty cells
emptycells++;
}
else
{
table2.AddCell(new PdfPCell(new Phrase("Cell " + i)));
}
}
// Add all empty cells at the end
for (int i = 0; i < emptycells; i++)
{
PdfPCell empty2 = new PdfPCell();
empty2.Border = PdfPCell.NO_BORDER;
table2.AddCell(empty2);
}
doc.Add(table2);
Finally, if you simply determine how many empty cells there will be, before creating the table, you can adjust the number of columns in the table.
(Again, you can adjust the table width, to get a result similar to the previous option.)
doc.Add(new Paragraph("Option 3:"));
// Determine the number of empty cells before hand
int emptycellsCounted = 2;
PdfPTable table3 = new PdfPTable(5 - emptycellsCounted);
table3.SpacingBefore = 10;
for (int i = 1; i <= 5; i++)
{
if (i == 2 || i == 3)
{
// Do nothing
}
else
{
table3.AddCell(new PdfPCell(new Phrase("Cell " + i)));
}
}
doc.Add(table3);
doc.Close();
You say there's an additional constraint: if a cell is empty, all following cells will also be empty. In that case, options 1 and 2 are the same.
The result of the 3 code samples:
Upvotes: 4