About UsCommunityTrainingContent DevelopmentContact

Blogs
Pluralsight
Course Schedule
Scott Allen
Craig Andera
Mark Baciak
Don Box
Keith Brown
John CJ
Tim Ewald
Jon Fancey
Jon Flanders
Vijay Gajjala
Kirill Gavrylyuk
Ian Griffiths
Martin Gudgin
Jim Johnson
John Justice
Mike Henderson
Joe Hummel
Matt Milner
Ted Neward
Fritz Onion
Brian Randell
Jeffrey Schlimmer
Aaron Skonnard
Dan Sullivan
Herb Sutter
Doug Walter
Jim Wilson
Mike Woodring

My Links
Home
Contact
Login

Blog Stats
Posts - 169
Stories - 0
Comments - 1078
Trackbacks - 219

Inter alia
My book
My wiki
Speaking
Webcasts

Archives
Apr, 2008 (3)
Mar, 2008 (6)
Feb, 2008 (4)
Jan, 2008 (2)
Dec, 2007 (1)
Nov, 2007 (3)
Oct, 2007 (1)
Sep, 2007 (4)
Aug, 2007 (1)
Jul, 2007 (3)
Jun, 2007 (3)
May, 2007 (2)
Apr, 2007 (3)
Mar, 2007 (4)
Feb, 2007 (2)
Jan, 2007 (3)
Dec, 2006 (1)
Nov, 2006 (3)
Oct, 2006 (6)
Sep, 2006 (2)
Aug, 2006 (7)
Jul, 2006 (1)
Jun, 2006 (5)
May, 2006 (2)
Apr, 2006 (2)
Mar, 2006 (5)
Feb, 2006 (3)
Jan, 2006 (4)
Dec, 2005 (6)
Nov, 2005 (3)
Oct, 2005 (6)
Aug, 2005 (6)
Jul, 2005 (4)
Jun, 2005 (4)
May, 2005 (2)
Apr, 2005 (8)
Mar, 2005 (4)
Feb, 2005 (5)
Jan, 2005 (2)
Dec, 2004 (2)
Nov, 2004 (6)
Oct, 2004 (7)
Sep, 2004 (6)
Aug, 2004 (3)
Jul, 2004 (5)
Jun, 2004 (3)
May, 2004 (1)

Post Categories
Personal(rss)
Technical(rss)



Wednesday, April 16, 2008

I've been working on a project recently where I had the need to randomly shuffle all of the rows in a DataTable. I wanted to do it with the DataTable itself instead of in the act of populating the DataTable for a couple of reasons: 1) I wanted to keep the DataTable in memory and shuffle it in place multiple times without going back to the source, and 2) I had multiple sources where data was coming from (SQL and XML) so I preferred to keep the randomization logic in one place. I also didn't want to copy all of the data (even though it was not a large amount) each time I shuffled, so I decided to use a DataView to display the data shuffled each time I needed it.

Here's the utility function I came up with - each time you call RandomizeDataTable it will return a newly shuffled DataView of all the data passed in through the DataTable. Note that because I reuse the added column "rndSortId" each time, any DataViews retrieved from previous calls to the method will have the new shuffle order. You could change this behavior by adding a new column each time with its own unique sort sequence.

As always, comments/improvements welcome – enjoy!

public static class DataSetUtilities

{

static Random _rand = new Random();

 

public static DataView RandomizeDataTable(DataTable dt)

{

// Create array of indices and populate with ordinal values

int[] indices = new int[dt.Rows.Count];

for (int i = 0; i < indices.Length; i++)

indices[i] = i;

 

// Knuth-Fisher-Yates shuffle indices randomly

for (int i = indices.Length - 1; i > 0; i--)

{

int n = _rand.Next(i + 1);

int tmp = indices[i];

indices[i] = indices[n];

indices[n] = tmp;

}

 

// Add new column to data table (if it's not there already)

// to store shuffle index

if (dt.Columns["rndSortId"] == null)

dt.Columns.Add(new DataColumn("rndSortId", typeof(int)));

int rndSortColIdx = dt.Columns["rndSortId"].Ordinal;

for (int i = 0; i < dt.Rows.Count; i++)

dt.Rows[i][rndSortColIdx] = indices[i];

 

DataView dv = new DataView(dt);

dv.Sort = "rndSortId";

return dv;

}

}

posted @ 7:38 AM | Feedback (4)

Wednesday, April 09, 2008

For the attendees of tonight's user group talk in Waltham, you can grab the demos here. Thanks for coming, and for all the great questions!

posted @ 8:48 PM | Feedback (1)

Tuesday, April 01, 2008

I'll be speaking next Wednesday (April 9, 2008) at the Boston .NET User Group meeting in Waltham, MA. I'm going to look at the new control architecture in Silverlight 2 and talk about building reusable components in Silverlight, so if you're in the area, be sure to reserve the night - should be fun!

posted @ 8:32 AM | Feedback (1)

Monday, March 31, 2008

Here's a quick way to print the current row number in a Repeater control (I promised a conference talk attendee to post this recently):

<asp:Repeater ID="rep1" runat="server' DataSourceID="myds">
<ItemTemplate>
<%#(((RepeaterItem)Container).ItemIndex+1).ToString() %>
<-- other data binding expressions here... -->
</ItemTemplate>
</asp:Repeater>

posted @ 10:34 AM | Feedback (0)

Thursday, March 13, 2008

If you attended my talk today on New Features of ASP.NET 3.5 and Visual Studio 2008 at DevWeek 2008, you can grab the demos here.
Thanks for coming!
posted @ 7:29 AM | Feedback (4)

Wednesday, March 12, 2008

If you were at the user group meeting in London tonight for VBUG, here are the demos for the talk. Thanks for coming and for all the great questions!
posted @ 4:36 PM | Feedback (4)
 
If you attended my talk (or talks) today on Silverlight ASP.NET Controls and Silverlight Server Communication at DevWeek 2008, you can grab the demos here and here respectively.
Thanks for coming!
posted @ 7:53 AM | Feedback (2)

Tuesday, March 11, 2008

If you attended my talk today on ASP.NET Ajax Design at DevWeek 2008, you can grab the demos here.
Thanks for coming!
posted @ 7:22 AM | Feedback (2)

Monday, March 10, 2008

Thanks to all who attended today's pre-conference tutorial on Silverlight with myself and Ian Griffiths. You can grab the demos here.
Remember to stop by the Pluralsight booth if you were one of the t-shirt winners to collect your prize (and please stop by even if you weren't)!
posted @ 11:54 AM | Feedback (2)

Monday, February 25, 2008

While I'm in London at DevWeek, I will also be presenting at VBUG on March 12 on building user controls in Silverlight 2.0. Hope to see you there!
posted @ 10:32 AM | Feedback (0)


 
   
 
© 2004 Pluralsight.
Visual Design by Studio Creativa
Privacy Policy