fedeteka fedeteka - 1 month ago 23
Vb.net Question

iTextsharp and DataGrid color conflict

One DataGrid on my code personalized with code like

With MyDataGrid
.RowsDefaultCellStyle.BackColor = Color.White
.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
End With


Works fine.

Using Nuget I installed iTextsharp using Install-Package iTextSharp and then I added the lines

Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports iTextSharp.text.BaseColor


The code works and the PDF was created

But if I add the line

Imports iTextSharp.text.Font


A conflict arise on the lines

With MyDataGrid
.RowsDefaultCellStyle.BackColor = Color.White
.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
End With


Visual Studio says


Property iTextSharp.text.Font.Color as BaseColor
Get/set the color of this font
Reference to a non-shared member requires an object reference


I think Visual Studio confuses the Color.White property of the Datagrid with the one of iTextsharp's text.

But even using this code (outside the With MyDataGrid)

MyDataGrid.RowsDefaultCellStyle.BackColor = Color.White
MyDataGrid.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray


The conflict persist

How can i avoid this conflict?

Answer

You can force it to use the system colors you want by explicitly using System.Drawing colors, so it won't try and grab the color prefix from your other itextsharp references:

    With MyDataGrid
        .RowsDefaultCellStyle.BackColor = System.Drawing.Color.White
        .AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightGray
    End With
Comments