[英]Hide column from datagridview in a pdf file
我已將數據從數據庫導出到 datagridview,然后導出到 pdf 文件,我想刪除此文件中的一列,因為它是一張照片 - 我在單元格(System.Byte [])中只得到它的類型。
我試圖讓我的列在 datagridview 中不可見,但沒有奏效。 它對 pdf 文件沒有任何影響,只有 datagridview 中的列被隱藏了。
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,
BaseFont.CP1250, BaseFont.EMBEDDED);
PdfPTable pdfTable = new PdfPTable(dgv.Columns.Count);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
iTextSharp.text.Font text = new iTextSharp.text.Font(bf,10,iTextSharp.text.Font.NORMAL);
//Add header
foreach(DataGridViewColumn column in dgv.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, text));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdfTable.AddCell(cell);
}
//add datarow
foreach(DataGridViewRow row in dgv.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
//dgv.Columns[7].Visible = false;
pdfTable.AddCell(new Phrase(cell.Value.ToString(), text));
}
}
var savefiledialoge = new SaveFileDialog();
savefiledialoge.FileName = filename;
savefiledialoge.DefaultExt = ".pdf";
if(savefiledialoge.ShowDialog()==DialogResult.OK)
{
using(FileStream stream = new FileStream(savefiledialoge.FileName,FileMode.Create))
{
Document pdfdoc = new Document(PageSize.A4,10f,10f,10f,0f);
PdfWriter.GetInstance(pdfdoc, stream);
pdfdoc.Open();
pdfdoc.Add(pdfTable);
pdfdoc.Close();
stream.Close();
}
}
那是因為即使你讓它不可見,你仍然在循環中
所以你只需要在循環中創建一個條件來檢查列是否可見
像這樣 :
foreach(DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible) continue;
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, text));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdfTable.AddCell(cell);
}
//add datarow
foreach(DataGridViewRow row in dgv.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
if (!dgv.Columns[cell.ColumnIndex].Visible) continue;
//dgv.Columns[7].Visible = false;
pdfTable.AddCell(new Phrase(cell.Value.ToString(), text));
}
}
現在你可以讓你的列可見,它不會出現在 pdf 文件中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.