Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Shuffling DataGridView Rows

I have a DataGridView with thousands of records which I programmatically added them. There is no datasource involved in this case. Now I want to shuffle the records each time I click a button. How can that be achieved?

like image 969
user1295450 Avatar asked Nov 13 '22 02:11

user1295450


1 Answers

I would not recommend OrderBy(x => Guid.NewGuid()). Depending on the system, your Guid could be sequential. GUIDs are meant to be unique, not random. To shuffle a collection, you can call sort and passes in a Comparator that returns result at random (e.g. Random(-1, 1)).

like image 85
Steven Luu Avatar answered Dec 21 '22 04:12

Steven Luu