iTextSharp仅显示某些DataGridView列 [英] iTextSharp Display only certain DataGridView columns

查看:239
本文介绍了iTextSharp仅显示某些DataGridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用iTextSharp在PDF文件中仅显示某些DataGridView列。我知道您可以将整个GridView插入到PDF文档中。但是可以只显示DataGridView的某些列吗?

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?

这是迄今为止的代码:

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


推荐答案

我没有在我面前的IDE所以这可能无法编译完善,但您只需要在中为语句编写一些任意条件。该代码直接从我链接到的页面中解除,我刚刚添加了一些,如果语句检查列的名称并跳过。

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

编辑

根据你当前的代码和上面的代码,你想要的是:

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

而不是名称您可能需要使用 ID 或仅实际索引 j k 这将真的取决于您如何设置DGV。

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.

您还需要在内部中稍微更改您的逻辑, 循环。您正在正确检查null,但您仍然需要添加单元格,否则您的整个表可能会移位。

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

您需要做的另一个变化是实际的实际 PdfPTable 而不是:

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

您需要执行以下操作:

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

这篇关于iTextSharp仅显示某些DataGridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆