Smee
Smee

Reputation: 143

Selected Column Headers don't appear when Exporting DataGridview to PDF

The table outputs 21 columns which I want it to appear on the form and when when it is exported to PDF, I want only select 6 specific columns to output.

But in my case, the exported PDF won't export these columns completely like it shows 2 columns only and the data rows fill the next column headers. This datagridview is generated with SQL and I am using ItextSharp.

This is the datagridview: gridview is ok

And the PDF output: table with jumbled header columns

This is the code:

for (int i = 0; i < colNum; i++)
{
    Phrase ph = null;
    if (memberGrid.Columns[i].Name == "Name" || memberGrid.Columns[i].Name == "Gender" || memberGrid.Columns[i].Name == "Address" || memberGrid.Columns[i].Name == "Email Address" || memberGrid.Columns[i].Name == "Mobile No" || memberGrid.Columns[i].Name == "Home No.")
    {
        ph = new Phrase(memtable.Columns[i].ColumnName, FontFactory.GetFont("Arial", HeaderTextSize, iTextSharp.text.Font.BOLD));
        mainTable.AddCell(ph);
    }
}

for (int x = 0; x < memberGrid.Rows.Count; x++)
{
    for (int k = 0; k < memberGrid.Columns.Count; k++)
    {
        if (memberGrid.Columns[k].Name == "Name" || memberGrid.Columns[k].Name == "Gender" || memberGrid.Columns[k].Name == "Address" || memberGrid.Columns[k].Name == "Email Address" || memberGrid.Columns[k].Name == "Mobile No" || memberGrid.Columns[k].Name == "Home No.")
        {
            if (memberGrid[k, x].Value != null)
            {
                string s = memberGrid[k, x].Value.ToString().Trim();
                Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
                mainTable.AddCell(ph);
            }
        }
    }
}

How can my table appear properly?

Upvotes: 1

Views: 927

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125322

When working with PdfTable and when you call AddCell, cells are added to table in the order that you add them. For example if you create a table with 3 columns and add values 1,2,3,4,5,6 then 1,2,3 will add in first row and 4,5,6 will add in second row.

You should correct some things in your code:

  1. Replace colNum with memberGrid.Columns.Count
  2. Replace memtable.Columns[i].ColumnName with memberGrid.Columns[i].HeaderText
  3. This statement if (memberGrid[k, x].Value != null) may cause similar problem if a column contains null value.

These are obvious problems of the code which 1 and 2 caused the problem in output.

Example:

The below example has been taken from iTextSharp - Introducing Tables:

PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);

Upvotes: 1

Related Questions