Description of serialization in C # for deep copy and DataGridView initialization refreshes

Source: Internet
Author: User
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 ();});
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.