【问题标题】:Hide column from datagridview in a pdf file在pdf文件中隐藏datagridview中的列
【发布时间】:2019-08-08 09:38:04
【问题描述】:

我已将数据从数据库导出到 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.Name=="whatever") continue;..}foreach(DataGridViewColumn column in dgv.Columns) { if (!column.Visible ) continue;..}

标签: c# mysql database visual-studio pdf


【解决方案1】:

那是因为即使你让它不可见,你仍然会在循环中得到它

所以你只需要在循环中创建一个条件来检查列是否可见

像这样:

        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 文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 2013-12-20
    • 2013-02-18
    • 2013-05-11
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多