导出选定的行和列 datagridview

Export selected rows AND columns datagridview

我有一个 datagridview,我只需要使用 itextsharp 将 selected 行和列导出为 pdf。我能够导出整个 datagridview,也只能导出 selected 行。

问题 1. 行以相反的顺序导出,而不是我检查的顺序。 (例如,我检查行 ID 1、5、8,在 pdf 中它是 8、5、1)

我不想改变SelectionMode,因为现在我使用FullRowSelect加载值到文本框并导出 selected 行。我在这里找到了一个可能的答案:Get selected Row/Columns Count without Setting Selection Mode 但我无法使用它..

问题 2。我需要用户 select 行和列进行导出。

这是导出 dgv 的代码:

private void exportDgvPDF(DataGridView dgvLoadAll, string filename)
   {
        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);

        iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL);

        Document doc = new Document(PageSize.A2.Rotate(), 1, 1, 1, 1);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
        doc.Open();

        // I need to remove 21 columns since I have lots invisible "useless" columns .. will work on that later
        PdfPTable pdftable = new PdfPTable(dgvLoadAll.ColumnCount - 21);

        for (int j = 0; j < dgvLoadAll.Columns.Count - 21; j++)
        {
            PdfPCell cell = new PdfPCell(new Phrase(dgvLoadAll.Columns[j].HeaderText, text));
            cell.BackgroundColor = BaseColor.LIGHT_GRAY;
            pdftable.AddCell(cell);
        }

        pdftable.HeaderRows = 0;
        for (int i = 0; i < dgvLoadAll.SelectedRows.Count; i++)
        //for (int i = 0; i < dgvLoadAll.Rows.Count; i++)
        {
            for (int k = 0; k < dgvLoadAll.Columns.Count - 21; k++)
            {

                if (dgvLoadAll[k, i].Value != null)
                {
                    pdftable.AddCell(new Phrase(dgvLoadAll.SelectedRows[i].Cells[k].Value.ToString(), text));
                    //pdftable.AddCell(new Phrase(dgvLoadAll[k, i].Value.ToString(), text));

                }
            }
        }

        //float[] widths = new float[] { 15f, 50f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f };

       // pdftable.SetWidths(widths);
        doc.Add(pdftable);
        doc.Close(); 
    }
private void exportDgvPDF(DataGridView dgvLoadAll, string filename)
{

        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);

        iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL);

        Document doc = new Document(PageSize.A2.Rotate(), 1, 1, 1, 1);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
        doc.Open();

        // I need to remove 21 columns since I have lots invisible "useless" columns .. will work on that later
        PdfPTable pdftable = new PdfPTable(dgvLoadAll.ColumnCount - 21);

        for (int j = 0; j < dgvLoadAll.Columns.Count - 21; j++)
        {
            PdfPCell cell = new PdfPCell(new Phrase(dgvLoadAll.Columns[j].HeaderText, text));
            cell.BackgroundColor = BaseColor.LIGHT_GRAY;
            pdftable.AddCell(cell);
        }


        pdftable.HeaderRows = 0;

    // i add foreach i hope this will help you 
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            for (int k = 0; k < dataGridView1.Columns.Count - 21; k++)
            {
                if (dgvLoadAll[k, i].Value != null)
                {
                 pdftable.AddCell(new Phrase(row.Cells[k].Value.ToString(), text));
                }
            }

        }


        doc.Add(pdftable);
        doc.Close(); 


        }

我明白了。这是我使用 itextsharp 将 selected 行和列导出为 pdf 的完整解决方案。

要select导出哪些列,我使用ColumnHeaderMouseClick事件。您需要使列不可排序。我将 selected 列索引添加到列表 columnList。如果已经有这个索引,则不会添加。

         private void dgvLoadAll_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

        DataGridViewColumn dataGridViewColumn = dgvLoadAll.Columns[e.ColumnIndex];

        if (columnsList.Contains(e.ColumnIndex))
        {
            columnsList.Remove(e.ColumnIndex);
            dataGridViewColumn.HeaderCell.Style.BackColor = SystemColors.Control;
        }
        else
        {  
            columnsList.Add(e.ColumnIndex);
            dataGridViewColumn.HeaderCell.Style.BackColor = SystemColors.Highlight;
        }
    }

类似我创建了一个列表行selected.

        foreach (DataGridViewRow r in dgvLoadAll.SelectedRows)
        {
            rowsselected.Add(r.Index);
        }

然后主要导出函数:

            BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
            iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL);

            Document doc = new Document(PageSize.A2.Rotate(), 1, 1, 1, 1);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
            doc.Open();

            // sort selected columns and rows indexes in order to be exported how they are viewed in datagridview
            columnsList.Sort();
            rowsselected.Sort();

            PdfPTable pdftable = new PdfPTable(columnsList.Count);
            foreach (int columnindex in columnsList)
            {
                PdfPCell cell = new PdfPCell(new Phrase(dgvLoadAll.Columns[columnindex].HeaderText, text));
                cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                pdftable.AddCell(cell);
            }

            pdftable.HeaderRows = 0;

            foreach (int rowindex in rowsselected)
            {
                foreach (int columnindex in columnsList)
                {
                    if (dgvLoadAll[columnindex, rowindex].Value != null)
                    {
                        pdftable.AddCell(new Phrase(dgvLoadAll.Rows[rowindex].Cells[columnindex].Value.ToString(), text));
                    }
                }
            }
            //float[] widths = new float[] { 15f, 50f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f };
            // pdftable.SetWidths(widths);
            doc.Add(pdftable);
            doc.Close();
            rowsselected.Clear();

您可以更改 select 列的方式,例如如果您需要自动对 datagridview 列进行排序,请制作一些复选框等。 已解决。