WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Just-In-Time Data Loading For DataGrids
Pages: 1, 2, 3, 4, 5, 6

Generating Five-Letter Customer IDs

Translating the record number to base 26 requires the following steps (remembering that we want the value of record 1 to be 'aaaab' (where a = zero):

1. Determine the values of each of the five places.

const int theBase =        26;
const int squared =        theBase * theBase;
const int cubed =          squared * theBase;
const int fourthPower =    cubed * theBase;

2. Initialize the five characters to 'a' (zero).

char char5 = 'a', char4 = 'a', char3 = 'a', char2 = 'a', char1 = 'a';

3. Divide the original number by the fourth power and use the result (if greater than zero) as an offset into the array of characters. Decrement the working value by the amount you've now assigned.

fourthPowerQuotient = workingValue / fourthPower;
if ( fourthPowerQuotient > 0 )
{
   char1 = letters[fourthPowerQuotient];
   workingValue -= fourthPowerQuotient * fourthPower
}

4. Repeat these steps for the cubedQuotient, the SquaredQuotient and the BaseQuotient. Finally, set the fifth character to letter at the offset of the remaining value.

char5 = letters[workingValue];

5. Return the five characters as a concatenated string.

return char1.ToString() + char2.ToString() +
   char3.ToString() + char4.ToString() + char5.ToString();

Hey! Presto! A base 26 string that represents the record number.

I use this application to put 100,000 or more records into the database and then test that the JIT access displays the data quickly and efficiently (which it does).

Next Steps

The only downside to the solution shown here is that we are bypassing the standard data binding. This is fine, but if you want your users to be able to substitute a different kind of grid (e.g., an Infragistics UltraGrid [2]), this approach won't work well. (Infragistics has its own solution to this, as you can imagine.) What is needed is a generic solution that decouples getting the data from populating the grid.

In my next column, I'll show a more generic solution that will work with any grid, at the cost of doing much more work tracking the data (and caching it if you want) yourself.


Footnotes


[1] This is a silly aphorism; of course the egg had to come first, because mutations must be genetic, not acquired, and speciation is an outcome of mutation. Return to text

[2] I will use the Infragistics UltraGrid for two good reasons. First, it is enormously popular and many of my clients use it. Second, they gave me a really nice free evaluation package that works with VS 2005.

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.


Read more Liberty on Beta 2 columns.

Return to WindowsDevCenter.com