The following small series to bring you a C # in the serialization of the implementation of deep copy, the implementation of DataGridView initialization Refresh method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Winfrom in the DataGridView of the cell in the edit will modify its data source, if we encounter such a scenario, refresh the data source to the original state, this time either the data source to regain the binding, or by copying a copy of the original file of data re-binding processing, The copy mode processing is described here.
The approximate code is as follows:
1. The target pair requires serialization and implements the ICloneable interface:
[Serializable]public class Dtocolumn:icloneable2. Implement interface Method Clone:public Object Clone () { using (MemoryStream ms = new Me Morystream (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. By copying a copy of the data to achieve the purpose of the refresh:
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 ();});