iTextSharp Display only certain DataGridView columns

老子叫甜甜 提交于 2019-12-13 08:29:40

问题


I want to display only certain DataGridView columns in a PDF file using iTextSharp. I know you can insert the whole GridView into the PDF document. But is it possible to only show certain columns of the DataGridView?

This is my code so far :

        iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

        PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
        table.SpacingBefore = 45f;
        table.TotalWidth = 300;
        table.DefaultCell.Phrase = new Phrase() { Font = fontTable };

        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
            table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable));

        }

        table.HeaderRows = 1;

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            for (int k = 0; k < dataGridView1.Columns.Count; k++)
            {
                if (dataGridView1[k, i].Value != null)
                {
                    table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
                }
            }
        }



        doc.Add(table);

回答1:


I don't have an IDE in front of me so this might not compile perfect but you just need to write some arbitrary conditions in your for statements. This code is lifted directly from that page that I linked to, I just added some if statements that check the column's name and skip those.

foreach (DataGridViewColumn c in GridView.Columns)
{
    if( c.Name == "Something" )
    {
        //Skip this column
        continue;
    }

    //process these columns
    //..
}

for (int i = 0; i < GridView.Rows.Count - 1; i++)
{
    for (int j = 0; j < GridView.Columns.Count - 1; j++)
    {
        if( GridView.Columns[j].Name == "Something" )
        {
            //Skip this column
            continue
        }

        //Process these columns
    }
}

EDIT

Based on your current code and the above you want something like:

iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
table.SpacingBefore = 45f;
table.TotalWidth = 300;
table.DefaultCell.Phrase = new Phrase() { Font = fontTable };

for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
    if(dataGridView1.Columns[j].Name == "Something")
    {
        continue;
    }
    table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText, fontTable));

}

table.HeaderRows = 1;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    for (int k = 0; k < dataGridView1.Columns.Count; k++)
    {
        if(dataGridView1.Columns[k].Name == "Something")
        {
            continue;
        }
        if (dataGridView1[k, i].Value != null)
        {
            table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
        }
    }
}
doc.Add(table);

Instead of Name you might need to use ID or just the actual indexes of j and k, that'll really be up to how you've setup your DGV.

You'll also want to change your logic slightly in the inner for loop. You are correctly checking for null but you still need to add a cell, otherwise your entire table could shift.

if (dataGridView1[k, i].Value != null)
{
    table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
}
else
{
    table.AddCell(new Phrase(""));
}

One more change you'll need to make is to the instantiation of the actual PdfPTable. Instead of:

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);

You'll want to do something like:

PdfPTable table = new PdfPTable(dataGridView1.Columns.Count - 2); //Subtract the number of columns that you are hiding


来源:https://stackoverflow.com/questions/24687739/itextsharp-display-only-certain-datagridview-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!