设置列类型ITEXTSHARP设置 [英] Setup column type ITEXTSHARP setup

查看:72
本文介绍了设置列类型ITEXTSHARP设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Some help
 
Sql column type is date
datagrid setup preview 19.09.2017
 
Datetimepicker format short value 19.09.2017
 
I need column  date 19.09.2017 00:00:00 setup just date 19.09.2017





我尝试过:





What I have tried:

//Creating iTextSharp Table from the DataTable data  
           //Console.WriteLine(kifDataGridView.ColumnCount); test za itext sharp  
           PdfPTable pdfTable = new PdfPTable(kifDataGridView.ColumnCount);  
           pdfTable.DefaultCell.Padding = 3;  
           pdfTable.WidthPercentage = 100;  
           pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
           pdfTable.DefaultCell.BorderWidth = 1;  
  
           float[] sirina = new float[] { 0f, 15f, 40f, 70f, 80f, 0f, 0f, 0f, 0f, 0f, 45f, 40f, 30f, 45f, 45f, 40f, 30f, 25f, 25f, 40f, 30f };  
           pdfTable.SetWidths(sirina);  
           BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
           iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 8);  
           iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 7, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.WHITE);  
  
  
  
           //Adding Header row  
           foreach (DataGridViewColumn column in kifDataGridView.Columns)  
           {  
               PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri2));  
               cell.BackgroundColor = new iTextSharp.text.BaseColor(243,70,5);  
               pdfTable.AddCell(cell);  
           }  
  
           //Adding DataRow  
           foreach (DataGridViewRow row in kifDataGridView.Rows)  
           {  
  
               foreach (DataGridViewCell cell in row.Cells)  
               {  
  
                   PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
                   pdfTable.AddCell(cell2);  
               }  
           }  

推荐答案

假设您使用的是最新版本的Visual Studio 2017:

Assuming you're using a recent version of Visual Studio 2017:
foreach (DataGridViewRow row in kifDataGridView.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        string text;
        switch (cell.Value)
        {
            case null:
            {
                text = string.Empty;
                break;
            }
            case DateTime date:
            {
                text = date.ToString("dd.MM.yyyy");
                break;
            }
            case DateTimeOffset date:
            {
                text = date.ToString("dd.MM.yyyy");
                break;
            }
            default:
            {
                text = cell.Value.ToString();
                break;
            }
        }
        
        pdfTable.AddCell(new PdfPCell(new Phrase(text, calibri)));
    }
}

模式匹配 - C#指南| Microsoft Docs [ ^ ]



编辑:您没有使用最新版本的Visual Studio 2017.您需要替换模式匹配 switch statement。

Pattern Matching - C# Guide | Microsoft Docs[^]

You are not using a recent version of Visual Studio 2017. You'll need to replace the pattern-matching switch statement.

string text;
if (cell.Value == null)
{
    text = string.Empty;
}
else if (cell.Value is DateTime)
{
    text = ((DateTime)cell.Value).ToString("dd.MM.yyyy");
}
else if (cell.Value is DateTimeOffset)
{
    text = ((DateTimeOffset)cell.Value).ToString("dd.MM.yyyy");
}
else
{
    text = cell.Value.ToString();
}


这篇关于设置列类型ITEXTSHARP设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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