简体   繁体   English

在 pdf 文件中隐藏 datagridview 中的列

[英]Hide column from datagridview in a pdf file

I have exported data from database to a datagridview and then to a pdf file and I want to delete one column in this file because it is a photo - I get only type of it in a cell (System.Byte[]).我已将数据从数据库导出到 datagridview,然后导出到 pdf 文件,我想删除此文件中的一列,因为它是一张照片 - 我在单元格(System.Byte [])中只得到它的类型。

I've tried to make my column invisible in datagridview but it didn't worked.我试图让我的列在 datagridview 中不可见,但没有奏效。 It didn't have any impact on a pdf file, only column in datagridview had become hidden.它对 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();
            }
        }

That's because even if you're making it invisible you still getting it in the loop那是因为即使你让它不可见,你仍然在循环中

so you just need to make a condition in your loop to check if the column is visible or not所以你只需要在循环中创建一个条件来检查列是否可见

like this :像这样 :

        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));
            }
        }

and now you can make your column to visible and it won't appear in the pdf file现在你可以让你的列可见,它不会出现在 pdf 文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM