0

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.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
Sturgus
  • 666
  • 4
  • 18
  • 1
    I don't think DataGridViewCellStyle is marked as serializable, so it doesn't know the instructions needed to create a new object. – LarsTech Apr 19 '16 at 15:32
  • Thanks @LarsTech. It's true that "Implements ISerializable" is not part of its MSDN definition and I also read that user-scoped settings must be serializable for them to work. Though, it would be nice if the project properties -> settings pane warned you when adding a "non-serializable" class/object to the settings so that future users don't waste as much time figuring this out as I did. – Sturgus Apr 19 '16 at 18:28
  • And so, you could add "DataGridViewCellStyles are not serializable and as such, are not retrievable application settings", and I'll accept it as the correct answer. – Sturgus Apr 19 '16 at 18:29
  • No controls are serializable. You can write a small class representing a "StyleSet" though, save user options to a collection of them and serialize it manually. – Ňɏssa Pøngjǣrdenlarp Apr 19 '16 at 19:40

1 Answers1

0

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.

jefff
  • 73
  • 1
  • 9