How do I serialize Datagridview data in C#?
-
http://stackoverflow.com/questions/9127253/why-are-system-windows-forms-control-not-marked-as-serializable – Dhaval Patel Jun 05 '14 at 08:31
-
`DataGridView` shows *content*, you have to serialize content, not `DataGridView` itself. If you want to serialize `DataGridView` properties (layout, column styles, etc), then make them a part of content. – Sinatr Jun 05 '14 at 08:40
2 Answers
I recently faced the need to serialize a DataGridView, but I have not found an objective answer here on how to resolve this demand. I know that a DataGridView is a view and not just data. So, thinking and testing, I got a practical solution, fast and very simple, which, for me, was enough, with the advantage of being able to record and retrieve any type of object that appeared in the columns of the DataGridView, such as images:
- to save the DataGridView data:
1.1) create a two-dimensional array of objects with the same dimensions as the DataGridView:
object[,] dataGridViewObjectsArray = new object[myDataGridView.Rows.Count, myDataGridView.Columns.Count];
1.2) transfer each row and column of the DataGridView to the same positions in the object array:
for (int x = 0; x < myDataGridView.Rows.Count; x++)
for (int y = 0; y < myDataGridView.Columns.Count; y++)
dataGridViewObjectsArray[x, y] = myDataGridView.Rows[x].Cells[y].Value;
1.3) save the objects array (Binary in my case, using the great solution from Dainel Schroeder, from Regina, SK, Canada):
BinarySerialization.WriteToBinaryFile<object[,]>(pathAndNameToObjectsArray, dataGridViewObjectsArray);
- to restore the DataGridView data:
2.1) restore the binary saved objects array (using the same great solution from Dainel Schroeder, from Regina, SK, Canada):
object[,] dataGridViewObjectsArray = BinarySerialization.ReadFromBinaryFile<object[,]>(pathAndNameToObjectsArray);
2.2) empty the DataGridView rows:
myDataGridView.Rows.Clear();
2.3) add a blank line to the DataGridView for each row in the object array and transfer back each column of the object array to the same positions in the DataGridView:
for (int x = 0; x < dataGridViewObjectsArray.GetLength(0); x++)
{
myDataGridView.Rows.Add();
for (int y = 0; y < dataGridViewObjectsArray.GetLength(1); y++)
myDataGridView.Rows[x].Cells[y].Value = dataGridViewObjectsArray[x, y];
}

- 11
- 3
Serialization is used for data and that is what it should be able to serialize. You`re trying to serialize a view, which it's purpose is to show data to the user. What you actually need to do is to serialize the data that is populating the DataGridView.
OT You answered your own question: Type 'System.Windows.Forms.DataGridView' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. You cannot serialize DataGridView as it is not marked as serializable in the framework.
You can attempt to create a Control that does exactly what DataGridView does and have custom Serialize/Deserialize methods or implement it that way that it can be serialized using .NET serializers. But this is a wrong approach IMHO.
Stick to serializing data and repopulating the view.

- 91
- 1
- 2
-
Yes, I want to Serialize the data !how do I do that? I couldn't find it here – user3613422 Jun 05 '14 at 08:47
-
The data for a DataGridView can be found in the DataSource property of the DataGridView, but the property is object. You actually need to serialize the object that is attributed to this variable or if bindings are used the one that the binding points to from the DataContext. `
.... ` – Razvan Popa Jun 27 '14 at 08:19