SoFunction
Updated on 2025-03-06

Serialization in C# implements deep copy and implements DataGridView initialization and refresh method

The data source in the cell in the DataGridView in winfrom will be modified when editing. If we encounter such a scenario, refresh the data source to the original state, at this time, either re-acquire the data source, or copy the data from the original file and then bind it. Here we introduce the copy method processing.

The general code is as follows:

1. The target pair needs to be serialized and implement the ICloneable interface:

[Serializable]
public class DtoColumn : ICloneable2.Implementing interface methodClone: 


public object Clone()
{
    using (MemoryStream ms = new MemoryStream(capacity))
    {
      object CloneObject;
      BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext());
      (ms, this);
      (0, );      
      CloneObject = (ms);       
      ();
      return CloneObject;
    }
}

3. Copy a copy of data to achieve the purpose of refreshing:

private List <  > DeepCloneData(List <  > rawdata) {
  return (x = >()).Cast <  > ().ToList()
}

this.(() = >
{
  this. = DeepCloneData(CloneInitialColumnData);
  this.();
});

The above article C# is the method of serializing and implementing deep copying and initializing and refreshing DataGridView is all the content I share with you. I hope you can give you a reference and I hope you can support me more.