Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);  
               }  
           }  
Posted
Updated 4-Jun-18 4:11am
v2
Comments
[no name] 3-Jun-18 13:16pm    
I don't see any "dates" in your code; why is this a "date" question?
Goran Bibic 5-Jun-18 2:26am    
Date is selected from SQL
Goran Bibic 5-Jun-18 2:26am    
Column 3 date sql data type

1 solution

Assuming you're using a recent version of Visual Studio 2017:
C#
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)));
    }
}
Pattern Matching - C# Guide | Microsoft Docs[^]

EDIT: You are not using a recent version of Visual Studio 2017. You'll need to replace the pattern-matching switch statement.
C#
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();
}
 
Share this answer
 
v2
Comments
Goran Bibic 5-Jun-18 2:25am    
Severity Code Description Project File Line Suppression State
Warning CS0164 This label has not been referenced BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 802 Active
Error CS1003 Syntax error, ':' expected BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 802 Active
Error CS1003 Syntax error, ':' expected BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 807 Active
Error CS0151 A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 795 Active
Error CS0119 'DateTime' is a type, which is not valid in the given context BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 802 Active
Error CS0103 The name 'date' does not exist in the current context BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 804 Active
Error CS0119 'DateTimeOffset' is a type, which is not valid in the given context BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 807 Active
Error CS0140 The label 'date' is a duplicate BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 807 Active
Error CS0103 The name 'date' does not exist in the current context BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 809 Active
Error CS8070 Control cannot fall out of switch from final case label ('default:') BSS C:\Users\Bibic Goran\Documents\Visual Studio 2015\Projects\BSS\BSS\mp_racun.cs 812 Active
Richard Deeming 5-Jun-18 9:00am    
"Assuming you're using a recent version of Visual Studio 2017"

You are not using a recent version of Visual Studio 2017.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900