Introduction to the method of implementing deep copy and initial refresh of DataGridView through serialization in C#

黄舟
Release: 2017-03-21 11:52:47
Original
1557 people have browsed it

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;
    }
}
Copy after login

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();
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!