Reputation: 7611
I have a list of objects in C# that have a property called 'Sequence'. This list is then bound to a DataGridView, ordering by this Sequence.
The user is able to drag and drop rows - so if we have something like the following:
Name,Sequence
Test1,1
Test2,2
Test3,3
Test4,4
Test5,5
Dragging 'Test1' to between 'Test3' and 'Test4' should modify the sequence of anything effected to be the following:
Name,Sequence
Test2,1
Test3,2
Test1,3
Test4,4
Test5,5
I can't quite figure out the algorithm to use to update the list when they drag and drop a row. A simple way would be to loop through all the rows in the grid and and update the sequence to be the row index + 1, but I think this will add some overhead.
Upvotes: 0
Views: 565
Reputation: 726489
See if you can avoid storing Sequence
with the object, otherwise a possibility of it getting out of sequence will always remain. You can pair up an object with its sequence for downstream consumption when enumerating through the container:
class MyData {
string Name {get; set;}
}
class DataWithSequence {
MyData Data {get;set;}
int Sequence{get;set;}
}
...
IList<MyData> list = new List<MyData>();
IEnumerable<DataWithSequence> GetData() {
return list.Select((d,i)=> new DataWithSequence {Data=d, Sequence=i+1});
}
Upvotes: 0
Reputation: 273179
but I think this'll add some overhead.
Very, very little.
And it has the benefit of not requiring any magic.
Upvotes: 2