最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#中序列化实现深拷贝,实现DataGridView初始化刷新的方法
时间:2022-06-25 07:52:34 编辑:袖梨 来源:一聚教程网
winfrom中DataGridView在的单元格在编辑时候会修改它的数据源的,如果我们遇到这样一种情景,刷新数据源到原始状态,这个时候要么数据源的重新获取绑定,要么通过拷贝一份原始档的数据再绑定处理,这里介绍拷贝方式处理。
大致代码如下:
1.目标对需要序列化,并实现ICloneable 接口:
代码如下 | 复制代码 |
[Serializable] publicclassDtoColumn : ICloneable2.实现接口方法Clone:
publicobjectClone() { using(MemoryStream ms =newMemoryStream(capacity)) { objectCloneObject; BinaryFormatter bf =newBinaryFormatter(null,newStreamingContext(StreamingContextStates.Clone)); bf.Serialize(ms,this); ms.Seek(0, SeekOrigin.Begin); CloneObject = bf.Deserialize(ms); ms.Close(); returnCloneObject; } } |
3. 通过拷贝一份数据来达到刷新的目的:
代码如下 | 复制代码 |
privateList < dto.DtoColumn > DeepCloneData(List < dto.DtoColumn > rawdata) { returnrawdata.Select(x = >x.Clone()).Cast < dto.DtoColumn > ().ToList() }
this.dataGridView1.DoThreadPoolWork(() = > { this.dataGridView1.DataSource = DeepCloneData(CloneInitialColumnData); this.dataGridView1.Refresh(); }); |
相关文章
- 《世界启元》S6赛季芝诺周瑜队搭配攻略 06-16
- 《世界启元》S6赛季新英雄达尔文技能分析及配队推荐 06-16
- 《纸嫁衣3鸳鸯债》全章节超详细攻略 06-16
- 杠杆、流动性与多头:无人预见的山寨币反弹格局 06-16
- 《战争警戒》美军卡组搭配攻略 06-16
- 《职场浮生记》技能加点攻略 06-16