简体   繁体   中英

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[]).

I've tried to make my column invisible in datagridview but it didn't worked. It didn't have any impact on a pdf file, only column in datagridview had become hidden.

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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