问题
I want to display only certain DataGridView columns in a PDF file using iTextSharp. I know you can insert the whole GridView into the PDF document. But is it possible to only show certain columns of the DataGridView?
This is my code so far :
iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
table.SpacingBefore = 45f;
table.TotalWidth = 300;
table.DefaultCell.Phrase = new Phrase() { Font = fontTable };
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable));
}
table.HeaderRows = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
}
}
}
doc.Add(table);
回答1:
I don't have an IDE in front of me so this might not compile perfect but you just need to write some arbitrary conditions in your for
statements. This code is lifted directly from that page that I linked to, I just added some if
statements that check the column's name and skip those.
foreach (DataGridViewColumn c in GridView.Columns)
{
if( c.Name == "Something" )
{
//Skip this column
continue;
}
//process these columns
//..
}
for (int i = 0; i < GridView.Rows.Count - 1; i++)
{
for (int j = 0; j < GridView.Columns.Count - 1; j++)
{
if( GridView.Columns[j].Name == "Something" )
{
//Skip this column
continue
}
//Process these columns
}
}
EDIT
Based on your current code and the above you want something like:
iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
table.SpacingBefore = 45f;
table.TotalWidth = 300;
table.DefaultCell.Phrase = new Phrase() { Font = fontTable };
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if(dataGridView1.Columns[j].Name == "Something")
{
continue;
}
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable));
}
table.HeaderRows = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if(dataGridView1.Columns[k].Name == "Something")
{
continue;
}
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
}
}
}
doc.Add(table);
Instead of Name
you might need to use ID
or just the actual indexes of j
and k
, that'll really be up to how you've setup your DGV.
You'll also want to change your logic slightly in the inner for
loop. You are correctly checking for null but you still need to add a cell, otherwise your entire table could shift.
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
}
else
{
table.AddCell(new Phrase(""));
}
One more change you'll need to make is to the instantiation of the actual PdfPTable
. Instead of:
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
You'll want to do something like:
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count - 2); //Subtract the number of columns that you are hiding
来源:https://stackoverflow.com/questions/24687739/itextsharp-display-only-certain-datagridview-columns