How to serialize controls

放肆的年华 提交于 2019-12-02 03:38:20
Jeremy Thompson

Can I serialize listview and propertygrid, or is there any options to do that?

Here is how to serialize a ListView:
http://www.codeproject.com/Articles/3335/Persist-ListView-settings-with-serialization

Here is how to serialize a PropertyGrid:
http://www.codeproject.com/Articles/27326/Load-and-Save-Data-Using-PropertyGrid

My advice is to do it properly, and I think the correct solution would be to serialize the business objects that the ListView and PropertyGrid are bound too. Separate the business logic from the GUI, then its really easy!

Edit (after OP edited question to show code):

To Save Data to the XML file:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BinFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream FS = new System.IO.FileStream("C:\\tv.xml", IO.FileMode.Create);
BinFormatter.Serialize(FS, new ArrayList(listview1.Items));
FS.Close();

To Read Data in from an XML file:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BinFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
string fname;
System.IO.FileStream FS = new System.IO.FileStream("C:\\tv.xml", IO.FileMode.Open);
listview1.Items.AddRange(BinFormatter.Deserialize(FS).ToArray(typeof(ListViewItem)));
FS.Close();

Here's how you do it with a PropertyGrid: PropertyGrid.Serialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!