Home >Backend Development >C#.Net Tutorial >Introduction to the method of implementing deep copy and initial refresh of DataGridView through serialization in C#
The following editor will bring you an article on how to implement deep copy through serialization in C# and implement initial refresh of DataGridView. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let's follow the editor and take a look.
The cell in the DataGridView in winfrom will modify its data source when editing. If we encounter such a situation, refresh the data source to the original state. At this time Either the data source is re-obtained for binding, or the data of the original file is copied and then bound. The copy method is introduced here.The approximate code is as follows:
1. The target pair needs to be serialized and implement ICloneable Interface:
[Serializable] public class DtoColumn : ICloneable2.实现接口方法Clone: public object Clone() { using (MemoryStream ms = new MemoryStream(capacity)) { object CloneObject; BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone)); bf.Serialize(ms, this); ms.Seek(0, SeekOrigin.Begin); CloneObject = bf.Deserialize(ms); ms.Close(); return CloneObject; } }
3. Refresh by copying a copy of data:
private List < dto.DtoColumn > DeepCloneData(List < dto.DtoColumn > rawdata) { return rawdata.Select(x = >x.Clone()).Cast < dto.DtoColumn > ().ToList() } this.dataGridView1.DoThreadPoolWork(() = > { this.dataGridView1.DataSource = DeepCloneData(CloneInitialColumnData); this.dataGridView1.Refresh(); });
The above is the detailed content of Introduction to the method of implementing deep copy and initial refresh of DataGridView through serialization in C#. For more information, please follow other related articles on the PHP Chinese website!