【问题标题】:iTextSharp Display only certain DataGridView columnsiTextSharp 仅显示某些 DataGridView 列
【发布时间】:2014-07-10 23:07:16
【问题描述】:

我想使用 iTextSharp 仅显示 PDF 文件中的某些 DataGridView 列。我知道您可以将整个 GridView 插入 PDF 文档。但是是否可以只显示 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);

【问题讨论】:

  • 这个问题实际上不是 iTextSharp 问题,因为 iTextSharp 完全不知道 DataGridViewGridView 是什么。您在上面提到了两个,一个是 WinForms,一个是 ASP.Net(如果我没记错的话)。无论如何,要从这些到 iTextSharp,您需要有一个中间步骤,并且在此中间步骤中您需要删除列。
  • 也许你建议中间步骤会有所帮助......
  • 我假设您已经完成了该步骤,您有一个现有的网格(仍然不确定您使用的是哪一个)并已将其复制到 PDF 中,现在 你想限制列。既然你说你知道你可以做到这一点,我会假设你已经看到了示例代码并实现了它。如果不是这样,您真正的问题是如何将 .Net 网格转换为 PDF。
  • 我正在使用 SqlCe 3.5 和 C#2010。该表单用于生成报价,datagridview 列出了客户购买的所有商品。在表单上,​​用户将数据输入到一个空白的 datagridview 中,然后创建一个新表,并将所有数据插入其中(以保持所有引用的唯一性)。在表单上,​​用户可以查看所有数据,但在 PDF 上,我想限制插入的列(因为客户会看到你赚了多少钱等)有没有办法只显示某些列?
  • 是的。几乎每个人都对行和列使用多个 for 循环。在内部for 循环中,只需执行if column == &lt;my column&gt; then skip it 之类的操作,如果您还没有for 循环部分,请查看此stackoverflow.com/q/13587520/231316

标签: c# datagridview itextsharp


【解决方案1】:

我面前没有 IDE,所以这可能无法完美编译,但您只需要在 for 语句中编写一些任意条件即可。这段代码是直接从我链接到的那个页面中提取的,我只是添加了一些 if 语句来检查列的名称并跳过这些语句。

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

编辑

根据您当前的代码和以上您想要的内容:

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 或仅使用 jk 的实际索引,而不是 Name,这实际上取决于您如何设置 DGV。

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

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

您需要进行的另一项更改是实际PdfPTable 的实例化。而不是:

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

你会想做这样的事情:

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

【讨论】:

  • 上面的代码。 continuefor 循环中“跳过”该索引
  • 我已经尝试了代码,但它没有显示任何列
  • @user3521452,我已经根据您发布的示例更新了上面的代码。而不是查看 Name 值,您可能需要使用其他值,例如 ID 或实际循环的索引值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 2018-09-06
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多