使用itextsharp生成gridview pdf格式时如何设置备用行颜色?

时间:2018-08-15 15:12:32

标签: c# winforms pdf itext

我已经在pdf表中添加了一些条件,但是我想在其他条件中添加替代行颜色,而不仅仅是白色。您可以看到有四种条件可以更改行颜色,但是我想在其他条件下放置替代颜色。

我的代码如下所示

   for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

            for (int k = 0; k < 2; k++)
            {
                if (dataGridView1[k, i].Value != null)
                {
                    DateTime dateTime = DateTime.UtcNow.Date;
                    var tomorrow = dateTime.AddDays(1);
                    var tom1 = dateTime.AddDays(2);
                    var tom2 = dateTime.AddDays(3);                      
                    string data = dataGridView1.Rows[i].Cells[6].Value.ToString();
                    string dateVal = Convert.ToDateTime(dataGridView1[k, i].Value).ToString("MM/dd/yyyy");                      
                    PdfPCell cellc = new PdfPCell(new Phrase(dateVal, fontTable));
                    if (dateVal == dateTime.ToString("MM/dd/yyyy") && data != "FIN - ENIG               ")
                    {                                                    
                         cellc.BackgroundColor = new iTextSharp.text.Color(0, 240, 240);
                        pdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;                       
                    }
                    else if (data != "FIN - ENIG               " && dateVal == tomorrow.ToString("MM/dd/yyyy"))
                    {
                        cellc.BackgroundColor = new iTextSharp.text.Color(0, 240, 240);
                        pdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    }
                    else if (data != "FIN - ENIG               " && dateVal == tom1.ToString("MM/dd/yyyy"))
                    {
                        cellc.BackgroundColor = new iTextSharp.text.Color(0, 240, 240);
                        pdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    }
                    else if (data != "FIN - ENIG               " && dateVal == tom2.ToString("MM/dd/yyyy"))
                    {
                        cellc.BackgroundColor = new iTextSharp.text.Color(0, 240, 240);
                        pdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    }
                    else
                    {



                       // pdfTable.DefaultCell.BackgroundColor = BaseColor.WHITE;
                    }

                    pdfTable.AddCell(cellc);
                }

            }

0 个答案:

没有答案