Search code examples
vb.netvisual-studiodatagridviewcellstyle

Visual Studio user-scoped DataGridViewCellStyle setting not saving


I'm uncertain if this is a bug, a 'feature', or if I'm doing something incorrectly, but I'm trying to save a DataGridViewCellStyle as a user-scoped setting in a Windows Forms application coded in VB.NET. I'm able to get other settings to save and load correctly, but not any DataGridViewCellStyles.

I went so far as to set up a new application to test this, and I'm having the same issue:

Public Class Form1



  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        My.Settings.UserDGVCellStyle = New DataGridViewCellStyle
        My.Settings.UserDGVCellStyle.Font = New Drawing.Font("Times New Roman", 14, FontStyle.Italic)
        My.Settings.UserDGVCellStyle.BackColor = Color.Azure
        My.Settings.UserFont = New Drawing.Font("Times New Roman", 13, FontStyle.Italic)
        My.Settings.UserBackgroundColor = Color.Black
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        My.Settings.Save()
    End Sub
End Class

I put a code-break on the first and last lines. The My.Settings.UserbackgroundColor and My.Settings.UserFont settings persist when the application is closed and reopened, but My.Settings.UserDGVCellStyle always shows as being 'nothing' when the application is restarted (even though it shows as being

"{DataGridViewCellStyle { BackColor=Color [Azure], Font=[Font: Name=Times New Roman, Size=14, Units=3, GdiCharSet=1, GdiVerticalFont=False] }}"

during the My.Settings.Save() line).

I realize that instead of trying to save the DataGridViewCellStyle, I could replace it with a bunch of individual settings (i.e., Font, BackColor, Alignment, etc), but I would rather not if I don't have to, as I'll have to do it many times for my application.

On a (potentially) related note, I remember having this issue when saving a DataTable, but once I filled DataTable.TableName, the table persisted successfully.


Solution

  • DataGridViewCellStyles are not serializable, thus they are not retrievable application settings.

    There it is, now in Answer form. I came looking for the answer to this same problem, and you are right, it was a time-waster to me as well.