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