Pulling column names from a database. Column names aren't user friendly so was wanting to rename them on the pdf. Is there a way to do this?

  foreach (DataColumn column in dt.Columns)
                {


                    PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));
                    pCell.BackgroundColor = BaseColor.BLUE;
                    pCell.Padding = 6;
                    contentTable.AddCell(pCell);
                }

this is the code I'm using to pull the column names.

Recommended Answers

All 4 Replies

Just replace column.ColumnName with the names you want. I suggest using a dictionary to use for the conversion.

in the line

PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));

Will that not only change the column name off one? or change all columns to the same name?

I thought i might have to do something such as

foreach (DataColumn column in dt.Columns)
{   

String NewName = column.ColumnName;
String revisedName = "";
switch (NewName)
{

case "AppRef":
    revisedName ="Reference";
    break;
    default:
    break;

    etc for rest of columns
    }

 PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));
pCell.BackgroundColor = BaseColor.BLUE;
pCell.Padding = 6;
contentTable.AddCell(pCell);
}

That's kind of what I meant. Just instead of your switch, create a dictionary with the from and to values. Something like this:

var map = new Dictionary<string, string>();
map.Add("AppRef", "Reference");
// Etc.
PdfPCell pCell = new PdfPCell(new Phrase(map[column.ColumnName], TableHeaderFont));

O get you now, thank you, i'll try that now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.