I have a List
By each element is A List of A,
I in datagridview binding List B.l ast ();
Then I revised in the DGV, I think, compared with that by the last for
This is my final judgment when you save the function changes, if there are changes, add in List B a new listA,
To simulate the change history
Problem, after the last on the list B binding DGV list B last also changed, along than not,
I again in by the last binding DGV, temporary A temp, create A new List to store by the last
DGV change after, however, the temp value also changed, what is this? Who can help explain and solve this problem,
My ultimate goal is, if there is no change value, click save, don't add a new listA to by the equal judgment cannot be achieved, because the original by. The last also changed,
CodePudding user response:
The problem of deep and shallow copyCodePudding user response:
Because the class is a reference type,,,CodePudding user response:
Well, I guess this problem, thank you two, so how to solve? Leave a copy of the lastCodePudding user response:
The problem is A not ListIf you are not A class (class) but structure (struct), there will be no trouble you now
Datagridview data source should be a datatable, although List< graph save trouble and you use; T> , it's ok to c # object will not be cut off for you reference
CodePudding user response:
Change my A struct, I changed my place has A lot ofSo how to solve this problem?
I List Temp=new List It's no use, and then Add,,
CodePudding user response:
Although change the class into a struct is a statement of the problem, but also do not recommend to do that. After all, c # is an object-oriented programmingBut some also to change the traditional thinking and practices, such as you leave a copy
CodePudding user response:
That how to solve the problem of I now face?How do you and the original data contrast? The original data is changed,
CodePudding user response:
Public class User: ICloneable
{
///& lt; Summary>
///the data replication into deep copy, you need to implement ICloneable interface
///& lt;/summary>
///& lt; Returns>
Public object Clone ()
{
Return (Object) enclosing MemberwiseClone ();
}
}
CodePudding user response:
Datagridview data source should be a datatable, ListYou can take List