<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kerry D. Wong &#187; C Sharp (C#)</title>
	<atom:link href="http://www.kerrywong.com/tag/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrywong.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 00:51:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>POV And POV Image Encoder</title>
		<link>http://www.kerrywong.com/2010/08/19/pov-and-pov-image-encoder/</link>
		<comments>http://www.kerrywong.com/2010/08/19/pov-and-pov-image-encoder/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 23:37:35 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[AVR/Arduino]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Atmega328P]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[POV]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=2430</guid>
		<description><![CDATA[One of my recent projects was to build a POV display device. There are already many microcontroller based POV devices out there, but most of those I have seen use around eight LEDs and have fixed font types. So I thought of developing something that is larger (e.g. using more LEDs) and more flexible (e.g. [...]]]></description>
			<content:encoded><![CDATA[<p>One of my recent projects was to build a POV display device. There are already many microcontroller based <a href="http://en.wikipedia.org/wiki/Persistence_of_vision">POV</a> devices out there, but most of those I have seen use around eight <a href="http://en.wikipedia.org/wiki/Light-emitting_diode">LEDs</a> and have fixed font types. So I thought of developing something that is larger (e.g. using more LEDs) and more flexible (e.g. can display both text and images).<span id="more-2430"></span></p>
<p>Without using any <a href="http://en.wikipedia.org/wiki/Shift_register">shift registers</a>, a single ATmega328p chip can handle a around 20 I/O ports. I decided to use 16 LEDs, twice the number of LEDs as the ones you commonly see. This gives me at least four free pins for other purposes. You can probably build a POV with around 20 LEDs, but If you need more than that, you will have to use shift registers to expand the available IO pins or change to a chip that has more built-in IO pins (i.e. ATMega1280). But as you shall see later, the image generation process will be considerably more complex and the required memory storage will be much larger as well.</p>
<h3>POV Display and Its Synchronization</h3>
<p>In order to achieve a stable image output, the image frames generated by the POV device must be synchronized. A POV display can operate as a hand device (e.g. a wand) or be driven by a motor. When the POV device is built for hand use, it usually has a motion detector (from simple spring-in-cylinder switch to the more exotic accelerometer) so that the device can start displaying when the hand is waved. I decided to go with a motor driven design so that I can have more precise control over the output image. In this approach, image frames must be synchronized for each full rotation. A simple way to detect the motor rotation is to use a IR transmitter and receiver.</p>
<p>The following is the full design of my POV display (the left side shows the optical triggering mechanism mentioned above):</p>
<div id="attachment_2443" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_schematics.png"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_schematics-300x188.png" alt="16 LED POV Schematics" title="16 LED POV Schematics" width="300" height="188" class="size-medium wp-image-2443" /></a><p class="wp-caption-text">16 LED POV Schematics</p></div>
<p>And here is the finished circuit mounted on a motorized rotating platform:</p>
<div id="attachment_2453" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_1.jpg"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_1-300x225.jpg" alt="16 LED POV Display (top view)" title="16 LED POV Display (top view)" width="300" height="225" class="size-medium wp-image-2453" /></a><p class="wp-caption-text">16 LED POV Display (top view)</p></div>
<p>Here is a picture of the side view, you can see how the IR transmitter and receiver circuits are mounted. Whenever the transmitter and the receiver line up (it happens every rotation), the transistor would output a LOW signal to the ATMega328p (pin 28, Arduino analog pin 5) which indicates the beginning of a new frame. To reduce the light interference from the environment, the receiver is mounted on the rotor so it is facing downwards.</p>
<div id="attachment_2454" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_2.jpg"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_2-300x225.jpg" alt="16 LED POV Display (side view)" title="16 LED POV Display (side view)" width="300" height="225" class="size-medium wp-image-2454" /></a><p class="wp-caption-text">16 LED POV Display (side view)</p></div>
<h3>Image Encoder</h3>
<p>Building the circuit is only half of the game. In order to provide the most flexible and the richest displays, I decided to build a image encoder which can be used to automatically translates a binary image into the appropriate code that is needed for the POV display. For simple POV displays, the display patterns can be mapped directly into a binary two dimensional array, with 1 represents LED on and 0 represents LED off. But this approach is not practical when displaying larger images. For an image the size of 16&#215;100, 1600 bytes would be required to store the image information. This might not sound like much but that is almost all of ATMega328&#8242;s usable SRAM. Of course, you can always store the data structure off in Flash memory using <a href="http://www.arduino.cc/en/Reference/PROGMEM">PROGMEM</a>, but it is not the most efficient way to do so. A better approach would be to pack these pixels into bytes so that each byte can represent 8 pixel values.</p>
<p>This code is shown as below. This C# code works with both Microsoft&#8217;s .Net platform or Mono.Net and thus it can be run on either Windows or Linux:</p>
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace convertImg
{
	class Program
	{
		static void Main (string[] args)
		{
			if (args.Length != 1) return;

			Image img = Image.FromFile (args[0]);			

			Bitmap bm = new Bitmap (img);
			BitmapData bmd = bm.LockBits (new Rectangle (0, 0, img.Width, img.Height),
			                              ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

			byte[,] ay = new byte[img.Width, img.Height];
			byte[] compactedAy = new byte[2 * img.Width];

			for (int y = 0; y &lt; img.Height; y++) {
				for (int x = 0; x &lt; img.Width; x++) {
					byte b = Marshal.ReadByte (bmd.Scan0, y * bmd.Stride + x * 4);

					if (b == 0)
						ay[x, y] = 1;
					else
						ay[x, y] = 0;

					Console.Write (ay[x, y]);
				}

				Console.WriteLine ();
			}

			Console.Write (&quot;byte dispArray[] = {&quot;);

			for (int x = 0; x &lt; img.Width; x++) {
				if (x % 8 == 0) {
					Console.WriteLine ();
					Console.Write (&quot;\t&quot;);
				}

				compactedAy[2 * x] = (byte)(ay[x, 0] &lt;&lt; 7 |
				                            ay[x, 1] &lt;&lt; 6 |
				                            ay[x, 2] &lt;&lt; 5 |
				                            ay[x, 3] &lt;&lt; 4 |
				                            ay[x, 4] &lt;&lt; 3 |
				                            ay[x, 5] &lt;&lt; 2 |
				                            ay[x, 6] &lt;&lt; 1 |
				                            ay[x, 7]);			

				compactedAy[2 * x + 1] = (byte)(ay[x, 8] &lt;&lt; 7 |
				                                ay[x, 9] &lt;&lt; 6 |
				                                ay[x, 10] &lt;&lt; 5 |
				                                ay[x, 11] &lt;&lt; 4 |
				                                ay[x, 12] &lt;&lt; 3 |
				                                ay[x, 13] &lt;&lt; 2 |
				                                ay[x, 14] &lt;&lt; 1 |
				                                ay[x, 15]);

				if (x &lt; img.Width - 1) {
					Console.Write (&quot;{0,3} &quot;, compactedAy[2 * x]);
					Console.Write (&quot;, &quot;);
					Console.Write (&quot;{0,3} &quot;, compactedAy[2 * x + 1]);
					Console.Write (&quot;, &quot;);
				} else {
					Console.Write (&quot;{0,3} &quot;, compactedAy[2 * x]);
					Console.Write (&quot;, &quot;);
					Console.Write (&quot;{0,3} &quot;, compactedAy[2 * x + 1]);
				}
			}

			Console.WriteLine ();
			Console.WriteLine (&quot;};&quot;);
		}
	}
}
</pre>
<p>The code above converts a binary image into an array of packed bytes. Every vertical line consists of 16 pixels (since we are using 16 LEDs) which is translated into two consecutive bytes in the resulting array. In general, the more LEDs you have, the more space it would take for each encoded line. As you can see, making the number of LEDs divisible by eight enables us to use full bytes for each encoded line. It is possible to use number of LEDs that do not fall into this pattern, but the resulting code would be either more difficult if full byte utilization is desired or leave some unused space between each line if lines need to start on byte boundaries. </p>
<p>Here is our test image.</p>
<div id="attachment_2462" class="wp-caption aligncenter" style="width: 110px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/ImageToBeDisplayed.png"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/ImageToBeDisplayed.png" alt="Image to be Displayed" title="Image to be Displayed" width="100" height="16" class="size-full wp-image-2462" /></a><p class="wp-caption-text"> </p></div>
<p>Using the image above (in PNG format) as the input, the encoded image is outputted to the terminal (formatted as a C++ array)</p>
<pre class="brush: cpp;">
byte dispArray[] = {
    0, 2, 0, 30, 0, 252, 7, 240, 31, 48, 252, 48, 224, 48, 252, 48,
    31, 48, 7, 240, 0, 252, 0, 30, 0, 2, 0, 0, 0, 0, 0, 0,
    15, 254, 15, 254, 6, 0, 12, 0, 12, 0, 12, 0, 0, 0, 1, 240,
    7, 252, 14, 14, 12, 6, 12, 6, 12, 6, 12, 6, 6, 12, 255, 254,
    255, 254, 0, 0, 0, 0, 0, 0, 0, 0, 15, 248, 15, 252, 0, 14,
    0, 6, 0, 6, 0, 6, 0, 12, 15, 254, 15, 254, 0, 0, 0, 0,
    0, 0, 0, 0, 207, 254, 207, 254, 0, 0, 0, 0, 0, 0, 0, 0,
    15, 254, 15, 254, 6, 0, 12, 0, 12, 0, 12, 0, 14, 0, 7, 254,
    3, 254, 0, 0, 0, 0, 0, 0, 1, 240, 7, 252, 6, 12, 12, 6,
    12, 6, 12, 6, 12, 6, 6, 12, 7, 252, 1, 240, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 3, 192, 12, 48, 48, 12, 32, 4, 76, 2,
    76, 66, 128, 33, 128, 17, 128, 17, 128, 17, 76, 34, 76, 66, 32, 4,
    48, 12, 12, 48, 3, 192, 0, 0
};
</pre>
<p>and is ready to be consumed by the Arduino side of the code.</p>
<h3>Arduino Code</h3>
<p>And here is the code on the Arduino side (I used Netbeans as my IDE, and you may need to change a couple of the includes if you are using the IDE bundled with Arduino. For more information, please see <a href="http://www.kerrywong.com/2010/05/16/arduino-development-using-netbeans/">my previous article on this</a>.)</p>
<p>As you can see, the <i>dispArray</i> is plugged in directly from the output of the encoder shown above. Note that here I assumed the image size is 16&#215;100, if your image widths are different, then you will need to adjust the range of <i>curPos</i> accordingly.</p>
<p>The analogRead() statement in the main loop detects whether the IR transmitter and receiver are aligned. If so, the analog pin output would become lower as the transistor on the receiver side becomes saturated and when a pre-defined threshold is crossed, the decoding process begins. Depending on how fast the display spins, <i>colDelay</i> may need to be adjusted accordingly.</p>
<pre class="brush: cpp;">
#include &lt;binary.h&gt;
#include &lt;HardwareSerial.h&gt;
#include &lt;pins_arduino.h&gt;
#include &lt;WConstants.h&gt;
#include &lt;wiring.h&gt;
#include &lt;wiring_private.h&gt;
#include &lt;WProgram.h&gt;
#include &lt;EEPROM/EEPROM.h&gt;

const int p1 = 0; //2;
const int p2 = 1; //3;
const int p3 = 2; //4;
const int p4 = 3; //5;
const int p5 = 4; //6;
const int p6 = 5; //11;
const int p7 = 6; //12;
const int p8 = 7; //13;
const int p9 = 8; //14;
const int p10 = 9; //15;
const int p11 = 10; //16;
const int p12 = 14; //23;
const int p13 = 15; //24;
const int p14 = 16; //25;
const int p15 = 17; //26;
const int p16 = 18; //27;

const int triggerPin = 5; //28;

const int colDelay = 8;

int ledPins[] = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16};
int curPos = 0;

/** Image generator output */
byte dispArray[] = {
    0, 2, 0, 30, 0, 252, 7, 240, 31, 48, 252, 48, 224, 48, 252, 48,
    31, 48, 7, 240, 0, 252, 0, 30, 0, 2, 0, 0, 0, 0, 0, 0,
    15, 254, 15, 254, 6, 0, 12, 0, 12, 0, 12, 0, 0, 0, 1, 240,
    7, 252, 14, 14, 12, 6, 12, 6, 12, 6, 12, 6, 6, 12, 255, 254,
    255, 254, 0, 0, 0, 0, 0, 0, 0, 0, 15, 248, 15, 252, 0, 14,
    0, 6, 0, 6, 0, 6, 0, 12, 15, 254, 15, 254, 0, 0, 0, 0,
    0, 0, 0, 0, 207, 254, 207, 254, 0, 0, 0, 0, 0, 0, 0, 0,
    15, 254, 15, 254, 6, 0, 12, 0, 12, 0, 12, 0, 14, 0, 7, 254,
    3, 254, 0, 0, 0, 0, 0, 0, 1, 240, 7, 252, 6, 12, 12, 6,
    12, 6, 12, 6, 12, 6, 6, 12, 7, 252, 1, 240, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 3, 192, 12, 48, 48, 12, 32, 4, 76, 2,
    76, 66, 128, 33, 128, 17, 128, 17, 128, 17, 76, 34, 76, 66, 32, 4 of,
    48, 12, 12, 48, 3, 192, 0, 0
};

/**
 * delay ms * 0.1 milliseconds
 */
void delayMilliseconds(int ms) {
    for (int i = 0; i &lt; ms; i++) {
        delayMicroseconds(100);
    }
}

void setup() {
    for (int i = 0; i &lt; 16; i++) {
        pinMode(ledPins[i], OUTPUT);
        digitalWrite(ledPins[i], LOW);
    }
}

void loop() {
    if (analogRead(triggerPin) &lt; 800) {
        for (int curPos = 99; curPos &gt;= 0; curPos--) {
            byte b1 = dispArray[curPos * 2];
            byte b2 = dispArray[curPos * 2 + 1];

            for (int i = 0; i &lt; 8; i++) {
                digitalWrite(ledPins[i + 8], (b1 &amp; (1 &lt;&lt; i)) &gt;&gt; i);
                digitalWrite(ledPins[i], (b2 &amp; (1 &lt;&lt; i)) &gt;&gt; i);
            }

            delayMilliseconds(colDelay);
        }

        for (int i = 0; i &lt; 16; i++) {
            digitalWrite(ledPins[i], LOW);
        }
    }
}
</pre>
<h3>POV Display in Action</h3>
<p>Here&#8217;s the POV display in action. Using the programs in this article what you can show on this POV display is not just limited to characters, any binary image design that fits the image dimension (16&#215;100 in this example) can be used. If you use different images for different frames you can even create your own POV animation.</p>
<div id="attachment_2457" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_3.jpg"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2010/08/POV_3-300x225.jpg" alt="16 LED POV Display in Action" title="16 LED POV Display in Action" width="300" height="225" class="size-medium wp-image-2457" /></a><p class="wp-caption-text">16 LED POV Display in Action</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2010/08/19/pov-and-pov-image-encoder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Thread Barrier Implementation</title>
		<link>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/</link>
		<comments>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 01:03:38 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Barrier]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Multi-threading]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=1540</guid>
		<description><![CDATA[Sometimes a group of concurrently running threads may need to rendezvous at a certain point in time before they can further proceed. This situation commonly arises in areas like event simulation, where the events are synchronized via a clock event (see illustration below): In the above example, a number of threads start at different times [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes a group of concurrently running threads may need to rendezvous at a certain point in time before they can further proceed. This situation commonly arises in areas like event simulation, where the events are synchronized via a clock event (see illustration below):<span id="more-1540"></span><br />
<div id="attachment_1541" class="wp-caption aligncenter" style="width: 620px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2009/11/eventbarrier.gif"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2009/11/eventbarrier.gif" alt="Event Barrier" title="Event Barrier" width="610" height="176" class="size-full wp-image-1541" /></a><p class="wp-caption-text">Event Barrier</p></div></p>
<p>In the above example, a number of threads start at different times with the arrival of each clock event and wait till all threads finish running before the next clock event arrives.</p>
<p>Such <a href="http://en.wikipedia.org/wiki/Barrier_%28computer_science%29">barrier</a> can be implemented using various techniques with different level of difficulties. <a href="http://www.albahari.com/threading/part3.aspx#_Thread_Pooling">One possible implementation</a> can be found in  <a href="http://www.albahari.com/threading/">Joseph Albahari&#8217;s Threading in C#</a> where a monitor construct is used to wait on a synchronization object in the main thread after the worker threads are queued. </p>
<pre class="brush: csharp;">
    lock (workerLocker)
    {
      while (runningWorkers &gt; 0)
        Monitor.Wait (workerLocker);
    }
</pre>
<p>In each of the worker thread, the number of running workers is decremented when the thread finishes:</p>
<pre class="brush: csharp;">
    lock (workerLocker)
    {
      runningWorkers--;
      Monitor.Pulse (workerLocker);
    }
</pre>
<p>And when the running number of workers reaches zero, the monitor exist the wait condition and the main thread continues.</p>
<p>The wait on the locker object forms a barrier in the example above. The method illustrated here is simple and elegant. It can be used to create the barriers needed to synchronize the clock event mentioned earlier.</p>
<p>Threadpooling might not always be the optimal solution though. While the number of threads in a thread pool is typically bounded to a few dozens, it may incur excessive context switching penalties if the majority of the threads are CPU bound. In a computation intensive situation, a fixed number of threads approximating the number of computational cores would be more appropriate (for instance, four threads on a four core CPU).</p>
<p>Here I will show another relatively simple way to construct a barrier where only a predetermined maximum number of threads can reach at a given time. To achieve such a ceiling, a global semaphore is used (in this example three concurrent threads can enter the semaphore):</p>
<pre class="brush: csharp;">
public class Global
{
    public static Semaphore sp = new Semaphore(3, 3);
}
</pre>
<p>To simplify coding and shield the programmer from the synchronization mechanism details, I created an <strong>IEvent</strong> interface and an abstract <strong>EventBase</strong> as follows:</p>
<pre class="brush: csharp;">
interface IEvent
{
    void OnEvent(int arg);
}
</pre>
<pre class="brush: csharp;">
public abstract class EventBase : IEvent
{
    #region IEvent Members
    public void OnEvent(int arg)
    {
        Global.sp.WaitOne();
        Run(arg);
        Global.sp.Release();
    }
    #endregion

    public virtual void Run(int arg)
    {
    }
}
</pre>
<p>Users can simply implement their code that needs to block at the barrier by inheriting from <strong>EventBase</strong> and overridding the <strong>Run()</strong> method:</p>
<pre class="brush: csharp;">
public class Event1 : EventBase
{
    public override void Run(int arg)
    {
        Console.WriteLine(&quot;Thread {0}, arg {1}&quot;, Thread.CurrentThread.ManagedThreadId, arg);
        Thread.Sleep(1000);
    }
}
</pre>
<p>To illustrate how this barrier implementation can be used considering the following code:</p>
<pre class="brush: csharp;">
class Program
{
    delegate void MyEvent(int arg);

    static void Main(string[] args)
    {
        IEvent e = new Event1();
        MyEvent m = new MyEvent(e.OnEvent);

        List&lt;IAsyncResult&gt; l = new List&lt;IAsyncResult&gt;();

        for (int i = 0 ; i &lt; 6 ; i++)
        {
          IAsyncResult r = m.BeginInvoke(i, null, null);
          l.Add(r);
        }

        foreach (IAsyncResult r in l)
        {
            m.EndInvoke(r);
        }
    }
}
</pre>
<p>In the code above, we simulate six concurrent threads that need to be synchronized. The multi-threading is achieved via asynchronous invocation. Because the capacity of the semaphore is set to be 3 in this example and if this code is run on a quad-core machine, you will see that the six asynchronous threads are run in two batches, three at a time. And all the threads are synchronized at the end. Here is the sample output:</p>
<blockquote><p>
Thread 10, arg 1<br />
Thread 11, arg 2<br />
Thread 6, arg 0<br />
Thread 13, arg 4<br />
Thread 10, arg 5<br />
Thread 12, arg 3
</p></blockquote>
<p>Using this approach, the number of concurrently running threads can be precisely controlled by the maximum threads allowed in the semaphore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monodevelop on Ubuntu 9.04</title>
		<link>http://www.kerrywong.com/2009/07/22/monodevelop-on-ubuntu-9-04/</link>
		<comments>http://www.kerrywong.com/2009/07/22/monodevelop-on-ubuntu-9-04/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 04:00:04 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Linux/BSD]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=1408</guid>
		<description><![CDATA[I am running Ubuntu 9.04 64bit. One thing I noticed is that the integrated debugger is behaving quite flaky for the included Monodevelop 2.0 package. After some research, it turned out that a lot of people are having similar issues. So I followed the instructions given in these two blogs: Install Mono 2.4 on Ubuntu, [...]]]></description>
			<content:encoded><![CDATA[<p>I am running Ubuntu 9.04 64bit. One thing I noticed is that the integrated debugger is behaving quite flaky for the included <a href="http://monodevelop.com/">Monodevelop 2.0</a> package. After some research, it turned out that a lot of people are having similar issues.<span id="more-1408"></span></p>
<p>So I followed the instructions given in these two blogs: <a href="http://blog.ruski.co.za/page/Install-Mono-on-Ubuntu.aspx">Install Mono 2.4 on Ubuntu</a>,<br />
<a href="http://synpl.blogspot.com/2009/07/building-mono-and-monodevelop-from.html">Building Mono and MonoDevelop from source on Ubuntu 9.04 64bit</a> to re-install Monodevelop 2.0 and its dependencies from the source. </p>
<p>One thing you will need to pay special attention to is that for the monodevelop-debugger-mdb-2.0 to compile successfully, you will need mono-debugger version 2.4, instead of the latest version 2.4.1. Otherwise you will receive an error on <em>Thread.AbortInvocation</em> as there is a breaking change in mono-debugger 2.4.1 which added a parameter to the method (see <a href="http://lists.ximian.com/pipermail/mono-patches/2009-May/149225.html">mono debugger 2.4.1 change log</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/07/22/monodevelop-on-ubuntu-9-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poor Man&#8217;s Parallel Task Dispatcher</title>
		<link>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/</link>
		<comments>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 01:55:39 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Multi-threading]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=1392</guid>
		<description><![CDATA[Even though multi-core processors are almost ubiquitous nowadays, applications are slow to catch up. Of course, one could always re-write the applications in order to take the full advantages in a multi-core environment. But it is not an easy undertaking. For applications that performs rather repetitive tasks over a well defined set of data (e.g. [...]]]></description>
			<content:encoded><![CDATA[<p>Even though multi-core processors are almost ubiquitous nowadays, applications are slow to catch up. Of course, one could always re-write the applications in order to take the full advantages in a multi-core environment. But it is not an easy undertaking.<span id="more-1392"></span> For applications that performs rather repetitive tasks over a well defined set of data (e.g. image processing) it is relatively easy to utilize multiple cores with some simple modifications to the applications themselves. </p>
<p>For the type of applications that perform the same task over a large dataset for example,</p>
<pre class="brush: csharp;">
for (int i = 0 ; i &lt; num ; i++)
{
    //lengthy tasks
}
</pre>
<p>we could easily break down the <em>for loop</em> evenly into chunks that fit into the number of processors given. And for each of the subsets we could create a new process to perform the desired tasks. For instance, if we were to execute the above <em>for loop</em> in parallel on a duo-core processor, we could re-write each of the loops like the following:</p>
<pre class="brush: csharp;">
//core 1
for (int i = 0 ; i &lt; num ; i+=2)
{
    //lengthy tasks
}
</pre>
<pre class="brush: csharp;">
//core 2
for (int i = 1 ; i &lt; num ; i+=2)
{
    //lengthy tasks
}
</pre>
<p>By executing the application in separate processes, we eliminated certain race conditions that must be properly handled in a multi-threaded process and thus it is far easier to execute multiple processes concurrently when there is no inter-process communications involved than to handle multi-processing within a process via multi-threading. In a multi-process scenario, the operating system handles the concurrency issue behind the scene whereas in the multi-threading case the application developer must ensure the code regions are thread safe.</p>
<p>The following C# code shows how to dispatch tasks among different processes using asynchronous method invocations. Each process being invoked can identify its order by the extra integer parameter (line 36) passed to it. Thus we only needed to modify the target application to take this extra parameter into account (for instance, in a quad-core system, the first process would process items 1,5,9&#8230; and the second process would run items 2,6,10, and so on)</p>
<pre class="brush: csharp;">
using System;
using System.Diagnostics;
using System.Threading;

namespace MPDispatcher
{
    class Program
    {
        public delegate bool CmdDlg(string fn, string args);

        private bool RunCommand(string fn, string args)
        {
            bool r = true;
            Process p = new Process();

            p.StartInfo.FileName = fn;
            p.StartInfo.Arguments = args;

			Console.WriteLine(String.Format(&quot;Starting {0} with arguments {1}. Thread ID: {2}&quot;, fn, args, Thread.CurrentThread.ManagedThreadId));
            r = p.Start();
            p.WaitForExit();

            return r;
        }

		public bool DispatchCommand(int numOfThreads, string args)
        {
            bool r = true;

            IAsyncResult[] ars = new IAsyncResult[numOfThreads];
            CmdDlg[] dlgs = new CmdDlg[numOfThreads];

            for (int i = 0; i &lt; numOfThreads; i++)
            {
                dlgs[i] = new CmdDlg(RunCommand);
                ars[i] = dlgs[i].BeginInvoke(args, i.ToString(), null, null);
				Thread.Sleep(100);
            }

            bool finished = false;

			//wait till all processes are finished.
            while (!finished)
            {
                bool t = true;

                for (int i = 0; i &lt; numOfThreads; i++)
                    t = t &amp;&amp; ars[i].IsCompleted;   

                if (t) finished = true;

                Thread.Sleep(100);
            }

            for (int i = 0; i &lt; numOfThreads; i++)
            {
                r = r &amp;&amp; dlgs[i].EndInvoke(ars[i]);
            }

            return r;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

			if (args.Length &lt; 1 || args[0].ToUpper() == &quot;-H&quot;) {
				Console.WriteLine(&quot;Usage: MPDispatcher {app name} {app args}&quot;);
			} else {
				int numThreads = 4; //use 4 threads
				string argStr = string.Empty;

				for (int i = 1 ; i &lt; args.Length; i++) {
					argStr = String.Format(&quot;{0} {1}&quot;, argStr, args[i]);
				}

            	p.DispatchCommand(numThreads, argStr);
			}
        }
    }
}
</pre>
<p>The above code works under Windows and Linux (Mono). </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two Level Tree and Its Applications — II</title>
		<link>http://www.kerrywong.com/2008/12/22/two-level-tree-and-its-applications-%e2%80%94-ii/</link>
		<comments>http://www.kerrywong.com/2008/12/22/two-level-tree-and-its-applications-%e2%80%94-ii/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 00:37:27 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=480</guid>
		<description><![CDATA[In my previous post, I discussed how to merge and split a two level tree. Before moving on to discuss its applications, let us take a look at the output of the sample program I gave before.&#160; &#160;&#160;&#160; &#160;&#160;&#160; static void Main(string[] args) &#160;&#160;&#160; &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; TreeNode n1 = new TreeNode(&#34;t1&#34;); &#160;&#160;&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="/2008/12/21/two-level-tree-and-its-applications-i/">previous post</a>, I discussed how to merge and split a two level tree. Before moving on to discuss its applications, let us take a look at the output of the sample program I gave before.&nbsp;<span id="more-480"></span></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">static</span> <span style="color: blue; font-weight: bold;">void</span> Main(<span style="color: blue; font-weight: bold;">string</span>[] args)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t1&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t2&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n3 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t3&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n4 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t4&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n5 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t5&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> n6 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span>(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;t6&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> t1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> n1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(n2)<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(n3);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WriteLine(t1);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> t2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> n4<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(n5)<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(n6);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WriteLine(t2);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> t3 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> t1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(t2);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WriteLine(t3);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> t4 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> t3<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Remove(n4);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WriteLine(t4);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">TreeNode</span> t5 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> t4<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Remove(n1);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WriteLine(t5);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Console</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ReadKey();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>Here is the output from the main routine:</p>
<blockquote>
<div><font face="Courier New">(t1);t2;t3;<br />
(t4);t5;t6;<br />
(t1);t2;t3;t4;t5;t6;<br />
(t1);t2;t3;t5;t6;<br />
(t2);t3;t5;t6;</font></div>
</blockquote>
<p>As you can see, tree t1 is formed by adding nodes t2 and t3 and the resulting tree is a two-level tree with t1 being the root. Similarly, t4 is formed by adding nodes t5 and t6. We then combined this two tree we just created, note how node t4 changed to a leaf in order to satisfy the two level property. In the next two output lines, we removed nodes from the two level tree. Note the last line: when we removed the root node, a leaf node (t2) is chosen as the new root of the tree.</p>
<p>Two level tree is extremely useful in capturing relationships in real world applications. For instance, if a is related to b and c, and d is related to e and f. Now we have two sets of distinct relationships (both are two level trees):</p>
<blockquote>
<div><font face="Courier New">(a);b;c</font></div>
<div><font face="Courier New">(d);e;f</font></div>
</blockquote>
<p>Now suppose that we also know c is related to e, what would the new relationship be? We know that a, b, c, d, e, f are then all related. But how do we capture this kind of relationships programmatically? As it turned out, we can use two level trees. In the example we just discussed, the relationship between e and c are captured via combining the two trees. And thus we get the following two level tree:</p>
<blockquote>
<div><font face="Courier New">(a);b;c;d;e;f</font></div>
</blockquote>
<p>Since related items are not necessarily limited to just tree roots (e.g. c and e are both leafs), we needed to be able to refer to a tree by either its root or its nodes and this is why in my <a href="/2008/12/21/two-level-tree-and-its-applications-i/">previous post</a> I mentioned that it would be a lot easier if we allowed referencing a tree by either the root node or its leafs.</p>
<p>One benefit of using a two level tree to capture relationships is that finding out the related items is a constant time operation since all of the related items are in a two level tree and no further tree traversal is required. In a large data set, this algorithm becomes very effective since no recursion is required.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/12/22/two-level-tree-and-its-applications-%e2%80%94-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two Level Tree and Its Applications &#8212; I</title>
		<link>http://www.kerrywong.com/2008/12/21/two-level-tree-and-its-applications-i/</link>
		<comments>http://www.kerrywong.com/2008/12/21/two-level-tree-and-its-applications-i/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 02:19:59 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=474</guid>
		<description><![CDATA[A two level tree is a simple tree data structure. Unlike in a typical tree where the tree depths could be arbitrary, a two level tree has only two levels as its name suggests. Two level tree is also equivalent to a star.The following graph illustrates what a two level tree looks like: A two [...]]]></description>
			<content:encoded><![CDATA[<p>A two level tree is a simple <a href="http://en.wikipedia.org/wiki/Tree_(data_structure)">tree data structure</a>. Unlike in a typical tree where the tree depths could be arbitrary, a two level tree has only two levels as its name suggests. Two level tree is also equivalent to a <a href="http://en.wikipedia.org/wiki/Star_(graph_theory)">star</a>.<span id="more-474"></span>The following graph illustrates what a two level tree looks like:</p>
<p align="center"><img alt="Two Level Tree" src="/blog/wp-content/uploads/2008/12/twoleveltree1.png" /></p>
<p align="center">A two level tree</p>
<p align="left">There is no internal nodes in a two level tree. A two level tree has only a root node and a set of leaf nodes.</p>
<p align="left">There are two types basic of operations we can perform on a two level tree: we can Add a two level tree to another two level tree and thus form a new two level tree or we can remove a node or a set of nodes from a two level tree.</p>
<p align="left">To add a two level tree to another two level tree, we need to ensure that the combined tree remains a two level tree. when we say adding a tree, it means adding the &quot;whole tree&quot;. But in practice, we really do not care which part of the tree we refer to. For instance, the above tree can be referred to by A (the root node), or by any of the four children (e.g. C). Thus there are four scenarios when adding two trees together, depending on how the tree is referenced. The following graph illustrate this:</p>
<p align="center"><img alt="Merging Two Two Level Trees" src="/blog/wp-content/uploads/2008/12/twoleveltree2.png" /></p>
<p align="center">Merging Two Two Level Trees</p>
<p align="left">The above four different scenarios depend on how the tree is referenced (the shaded letters). We could mandate that a tree referenced by its root node and thus simplify the merging algorithm, but you will see in a later post that this approach adds more flexibility. The following code (C#) shows how the above steps are accomplished:&nbsp;</p>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New"><span style="font-weight: bold; color: blue">namespace</span> TwoLevelTree</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">{</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: blue">class</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">private</span> <span style="font-weight: bold; color: blue">string</span> _ID <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">null</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">private</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span> _parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">null</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">private</span> <span style="font-weight: bold; color: #2b91af">List</span><span style="font-weight: bold; color: #8080c0">&lt;</span><span style="font-weight: bold; color: #2b91af">TreeNode</span><span style="font-weight: bold; color: #8080c0">&gt;</span> _children <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">null</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: blue">string</span> ID</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">get</span> { <span style="font-weight: bold; color: blue">return</span> _ID; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">set</span> { _ID <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">value</span>; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span> Parent</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">get</span> { <span style="font-weight: bold; color: blue">return</span> _parent; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">set</span> { _parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">value</span>; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: #2b91af">List</span><span style="font-weight: bold; color: #8080c0">&lt;</span><span style="font-weight: bold; color: #2b91af">TreeNode</span><span style="font-weight: bold; color: #8080c0">&gt;</span> Children</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">get</span> { <span style="font-weight: bold; color: blue">return</span> _children; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">set</span> { _children <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">value</span>; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> TreeNode(<span style="font-weight: bold; color: blue">string</span> id)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _ID <span style="font-weight: bold; color: #8080c0">=</span> id;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span> Add(<span style="font-weight: bold; color: #2b91af">TreeNode</span> node)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (<span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">==</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (<span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">==</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">List</span><span style="font-weight: bold; color: #8080c0">&lt;</span><span style="font-weight: bold; color: #2b91af">TreeNode</span><span style="font-weight: bold; color: #8080c0">&gt;</span>();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Children<span style="font-weight: bold; color: #8080c0">.</span>Add(node);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; node<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">this</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (node<span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">!=</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">foreach</span> (<span style="font-weight: bold; color: #2b91af">TreeNode</span> n <span style="font-weight: bold; color: blue">in</span> node<span style="font-weight: bold; color: #8080c0">.</span>Children)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Children<span style="font-weight: bold; color: #8080c0">.</span>Add(n);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; n<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">this</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; node<span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">null</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">else</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Parent<span style="font-weight: bold; color: #8080c0">.</span>Children<span style="font-weight: bold; color: #8080c0">.</span>Add(node);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; node<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Parent;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (node<span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">!=</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">foreach</span> (<span style="font-weight: bold; color: #2b91af">TreeNode</span> n <span style="font-weight: bold; color: blue">in</span> node<span style="font-weight: bold; color: #8080c0">.</span>Children)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Parent<span style="font-weight: bold; color: #8080c0">.</span>Children<span style="font-weight: bold; color: #8080c0">.</span>Add(n);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; n<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Parent;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; node<span style="font-weight: bold; color: #8080c0">.</span>Children <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">null</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">return</span> <span style="font-weight: bold; color: blue">this</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span> Remove(<span style="font-weight: bold; color: #2b91af">TreeNode</span> node)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (node<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">!=</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>Children<span style="font-weight: bold; color: #8080c0">.</span>Remove(node);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">else</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span><span style="font-weight: bold; color: #8080c0">.</span>ID <span style="font-weight: bold; color: #8080c0">=</span> _children[<span style="color: teal">0</span>]<span style="font-weight: bold; color: #8080c0">.</span>ID;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _children<span style="font-weight: bold; color: #8080c0">.</span>RemoveAt(<span style="color: teal">0</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">foreach</span> (<span style="font-weight: bold; color: #2b91af">TreeNode</span> n <span style="font-weight: bold; color: blue">in</span> _children)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; n<span style="font-weight: bold; color: #8080c0">.</span>Parent <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">this</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">return</span> <span style="font-weight: bold; color: blue">this</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: blue">override</span> <span style="font-weight: bold; color: blue">string</span> ToString()</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">StringBuilder</span> sb <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">StringBuilder</span>();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (_parent <span style="font-weight: bold; color: #8080c0">==</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="font-weight: bold; color: #8080c0">.</span>Append(<span style="font-weight: bold; color: blue">string</span><span style="font-weight: bold; color: #8080c0">.</span>Format(<span style="font-weight: bold; color: #a31515">&quot;({0});&quot;</span>, _ID));</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (_children <span style="font-weight: bold; color: #8080c0">!=</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">foreach</span> (<span style="font-weight: bold; color: #2b91af">TreeNode</span> n <span style="font-weight: bold; color: blue">in</span> _children)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="font-weight: bold; color: #8080c0">.</span>Append(n<span style="font-weight: bold; color: #8080c0">.</span>ToString());</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">else</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="font-weight: bold; color: #8080c0">.</span>Append(<span style="font-weight: bold; color: blue">string</span><span style="font-weight: bold; color: #8080c0">.</span>Format(<span style="font-weight: bold; color: #a31515">&quot;{0};&quot;</span>, _ID));</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">if</span> (_children <span style="font-weight: bold; color: #8080c0">!=</span> <span style="font-weight: bold; color: blue">null</span>)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">foreach</span> (<span style="font-weight: bold; color: #2b91af">TreeNode</span> n <span style="font-weight: bold; color: blue">in</span> _children)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="font-weight: bold; color: #8080c0">.</span>Append(n<span style="font-weight: bold; color: #8080c0">.</span>ToString());</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">return</span> sb<span style="font-weight: bold; color: #8080c0">.</span>ToString();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">class</span> <span style="font-weight: bold; color: #2b91af">Program</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">static</span> <span style="font-weight: bold; color: blue">void</span> Main(<span style="font-weight: bold; color: blue">string</span>[] args)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n1 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t1&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n2 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t2&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n3 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t3&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n4 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t4&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n5 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t5&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> n6 <span style="font-weight: bold; color: #8080c0">=</span> <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">TreeNode</span>(<span style="font-weight: bold; color: #a31515">&quot;t6&quot;</span>);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> t1 <span style="font-weight: bold; color: #8080c0">=</span> n1<span style="font-weight: bold; color: #8080c0">.</span>Add(n2)<span style="font-weight: bold; color: #8080c0">.</span>Add(n3);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>WriteLine(t1);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> t2 <span style="font-weight: bold; color: #8080c0">=</span> n4<span style="font-weight: bold; color: #8080c0">.</span>Add(n5)<span style="font-weight: bold; color: #8080c0">.</span>Add(n6);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>WriteLine(t2);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> t3 <span style="font-weight: bold; color: #8080c0">=</span> t1<span style="font-weight: bold; color: #8080c0">.</span>Add(t2);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>WriteLine(t3);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> t4 <span style="font-weight: bold; color: #8080c0">=</span> t3<span style="font-weight: bold; color: #8080c0">.</span>Remove(n4);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>WriteLine(t4);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">TreeNode</span> t5 <span style="font-weight: bold; color: #8080c0">=</span> t4<span style="font-weight: bold; color: #8080c0">.</span>Remove(n1);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>WriteLine(t5);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">Console</span><span style="font-weight: bold; color: #8080c0">.</span>ReadKey();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">}</div>
<p><!--EndFragment--></p>
<p align="left">The &quot;remove&quot; operation is pretty simple as well. Bascically, when the node to be removed is leaf, we simply remove it. When it is root, we select one of the leave nodes to be root and forming a new two level tree.</p>
<p align="left">In the next post, I will show you one of the applications of a two level tree.</p>
<p align="left">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/12/21/two-level-tree-and-its-applications-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Obscure System.Web.Mail Error Message</title>
		<link>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/</link>
		<comments>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 01:08:05 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=345</guid>
		<description><![CDATA[Today I encountered a rather obscure error while maintaining some old ASP.Net code (Framework 1.1). One of our mail servers is going to be replaced with another one. So naturally we needed to change configurations for those applications that send emails using the old server. This change affected one of the applications I wrote a [...]]]></description>
			<content:encoded><![CDATA[<p>Today I encountered a rather obscure error while maintaining some old ASP.Net code (Framework 1.1).<span id="more-345"></span> One of our mail servers is going to be replaced with another one. So naturally we needed to change configurations for those applications that send emails using the old server.</p>
<p>This change affected one of the applications I wrote a while ago. Since I stored all the configuration information inside the web.config file, I thought it was just a simple matter of changing the server from &#8216;server1&#8242; to &#8216;server2&#8242; and then I was done. Well, as it turned out, the result was totally unexpected.</p>
<p>Upon finishing the configuration change, I decided to test the application even though I was sure that everything would be working just fine. I was wrong, instead of getting the expected results, I got the following error message while trying to send email through the application:</p>
<p><strong>Exception:</strong><br />
<span style="color: #800000;">Could not access &#8216;CDO.Message&#8217; object.</span></p>
<p><strong>InnerException:</strong><br />
<span style="color: #800000;">Exception has been thrown by the target of an invocation.InnerException:<br />
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)<br />
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)<br />
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)<br />
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object[] args)<br />
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)</span></p>
<p>Since the only thing changed was the server configuration, I thought that it was definitely a configuration problem of the new mail server. But that could not have been the problem as many applications had already been migrated over and they were working just fine.</p>
<p>After a Google search, it became apparent to me that many people out there are having <a href="http://www.systemwebmail.com/">the same issue</a>. But no information seemed to point to a definitive answer. And the tricks mentioned in various articles did not solve my particular problem. After some struggle, I found out that the culprit was actually my <strong>MailMessage.From</strong> field. Instead of an actual email address, I had always used some random names (e.g. system notification, etc.). And apparently, the spaces in the From field string would cause the error I mentioned above. And if I remove all the spaces in the From field, the exception goes away. Since the <strong>SmtpMail</strong> object is simply a wrapper for the Win32 CDO (Collaborative Data Objects), the error was poorly propagated back (like many of the errors in .Net as I mentioned previously. See <a href="/2008/01/11/the-elusive-filecopy/">1</a>, <a href="/2007/11/15/understanding-a-generic-error-occurred-in-gdi-error/">2</a>, <a href="/2007/10/23/lousy-gdi-error-messages/">3</a>).</p>
<p>Indeed, had the error message been something more clear like &#8220;<em>Error: From field must not contain any space</em>&#8220;, I would not have spent the good portion of the afternoon hunting for bugs (I still think it must have something to do with the new mail server&#8217;s configuration, since this code has been working with other mail servers).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Producer-Consumer: A Duplicate File Finder</title>
		<link>http://www.kerrywong.com/2008/09/05/producer-consumer-a-duplicate-file-finder/</link>
		<comments>http://www.kerrywong.com/2008/09/05/producer-consumer-a-duplicate-file-finder/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 01:08:16 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/09/05/producer-consumer-a-duplicate-file-finder/</guid>
		<description><![CDATA[Processes tend to benefit greatly from multi-core processors if they are CPU bound (i.e. computational intensive tasks). The actual speedup depends on the portion of the code that must remain sequential. The multi-core benefit diminishes for IO bound processes however since hard drive performance becomes the limiting factor. Unlike solid state drives (SSD), traditional hard [...]]]></description>
			<content:encoded><![CDATA[<p>Processes tend to benefit greatly from multi-core processors if they are CPU bound (i.e. computational intensive tasks). The actual speedup <a href="http://en.wikipedia.org/wiki/Amdahl%27s_Law">depends on the portion of the code that must remain sequential</a>.<span id="more-334"></span> The multi-core benefit diminishes for IO bound processes however since hard drive performance becomes the limiting factor. Unlike solid state drives (SSD), traditional hard drives are very good at handling large sequential reads/writes but handle random accesses poorly. In fact, parallelizing sequential read/write access can actually cause performance degradation since sequential data might be accessed by different threads and parallelizing the access makes the access pattern of each thread more random. So the performance of such IO bound task is largely determined by the IO sub-system performance.</p>
<p>For tasks that contain both intensive IO operations and CPU usages however it is desirable to parallelize the IO bound portion and the CPU bound portion of code so that maximum parallelism can be achieved.</p>
<p>An example of such IO and CPU intensive tasks is finding duplicate files in a file system. To find duplicate files, files or their hashes must be compared. In this post, I will discuss a couple of different approaches to achieve this using threading.</p>
<p><strong>1. Sequential find</strong><br />
First let us take a look at how to find duplicate files sequentially (all the sample code can be downloaded from <a href="/blog/wp-content/uploads/2008/09/dupfinder.zip">here</a>):</p>
<p>Suppose we have a function that returns a list of files within a directory (see GetFileList in FileUtils). In function FindDuplicates, we loop through the list of files and calculate the hash of each file. The file name and hash are used together as a key to a hash table. Whenever the same key is indexed the count is incremented by one indicating a duplicate file is found. Here is what the code looks like:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> FindDuplicates(<span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> files)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: blue; font-weight: bold;">string</span> fn <span style="color: blue; font-weight: bold;">in</span> files)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> h <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ComputeMD5(fn);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (h <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span> def <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetFileName(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FilePath <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetDirectoryName(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>LastModifiedDate <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Directory</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetLastWriteTime(fn);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span> fileInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span>(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> fileInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Length;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (Hash<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Contains(<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)))</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> Hash[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(def);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Hash[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> l;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(def);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Hash<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h), l);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>
<strong>2. ThreadPool</strong><br />
We could improve the performance by moving the hash calculation to separate threads. </p>
<p>The first approach to parallelize the task is to use a thread pool to queue the work of calculating file hashes. Since calculating file hash involves reading in the whole file (IO bound) and some calculation on the content, there is some inherent parallelism so using multiple threads could potentially improve performance.</p>
<p>The code below shows this approach:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> FindDuplicates(<span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> files)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> (<span style="color: blue; font-weight: bold;">int</span> n <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span>; n <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> files<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Count; n <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> _numOfThreads)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _autoEvents <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">AutoResetEvent</span>[_numOfThreads];</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> (<span style="color: blue; font-weight: bold;">int</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span>; i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> _numOfThreads; i<span style="color: rgb(128, 128, 192); font-weight: bold;">++</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (n <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> files<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Count)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _autoEvents[i] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">AutoResetEvent</span>(<span style="color: blue; font-weight: bold;">false</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">ThreadPool</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>QueueUserWorkItem(CalculateFileHash, <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileThreadInfo</span>(i, files[n <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> i]));</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _autoEvents[i] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">AutoResetEvent</span>(<span style="color: blue; font-weight: bold;">true</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">WaitHandle</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>WaitAll(_autoEvents);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">void</span> CalculateFileHash(<span style="color: blue; font-weight: bold;">object</span> stateInfo)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileThreadInfo</span> threadInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> stateInfo <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileThreadInfo</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> h <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ComputeMD5(threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Thread</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CurrentThread<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (h <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span> def <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetFileName(threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FilePath <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetDirectoryName(threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>LastModifiedDate <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Directory</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetLastWriteTime(threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span> fileInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span>(threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> fileInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Length;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (Hash<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Contains(<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)))</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> Hash[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(def);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Hash[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> l;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(def);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Hash<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;:&quot;</span>, h), l);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _autoEvents[threadInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ID]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Set();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p><strong><br />
3. Producer and Consumer</strong><br />
Even through the previous approach improved the duplicated file finding performance, it is still not optimum. For example, even though multiple threads are used to calculate file hashes, they are joined at the end before another set of calculation is queued. Also, the IO is operations are distributed across multiple threads and on some older hard drives handling IO in multiple threads can actually slow things down. An ideal solution would be to use a single thread for IO operation since IO is inherently sequential. </p>
<p>As it turned out, this problem is a classic <a href="http://en.wikipedia.org/wiki/Producer-consumer ">producer-consumer</a> problem. In this third approach, we will examine how to use the producer consumer model to solve the problem of finding duplicates. In this approach, the producer thread searches for files within a given directory. It opens each file as it encounters and read the content into a <a href="http://research.microsoft.com/~birrell/papers/ThreadsCSharp.pdf">ProducerConsuerQueue</a>. At the same time, the consumer computes the hash from the bytes stored in the queue and determines whether a file is duplicated or not. The code is slightly different than what you saw above as files are being added to the queue and consumed from the queue simultaneously.</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">ProducerConsumerQueue</span> _q <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">null</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> FindDuplicates(<span style="color: blue; font-weight: bold;">string</span> rootDir)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">using</span> (_q <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">ProducerConsumerQueue</span>())</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GetFiles(rootDir);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _q<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OutputDuplicates();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> GetFiles(<span style="color: blue; font-weight: bold;">string</span> rootDir)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span>[] files <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">null</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">try</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; files <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Directory</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetFiles(rootDir);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: blue; font-weight: bold;">string</span> fn <span style="color: blue; font-weight: bold;">in</span> files)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span> def <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileDataDefinition</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileName <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetFileName(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FilePath <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Path</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetDirectoryName(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>LastModifiedDate <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Directory</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetLastWriteTime(fn);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span> fileInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileInfo</span>(fn);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>FileSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> fileInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Length;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">FileStream</span> f <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileStream</span>(fn, <span style="color: olive; font-weight: bold;">FileMode</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Open);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">byte</span>[] bytes <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: blue; font-weight: bold;">byte</span>[f<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Length];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Read(bytes, <span style="color: teal;">0</span>, (<span style="color: blue; font-weight: bold;">int</span>) f<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Length <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> <span style="color: teal;">1</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Close();</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _q<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>EnqueueTask(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">FileBytes</span>(def, bytes));</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">catch</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">Exception</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span>[] dirs <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">null</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">try</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dirs <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Directory</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetDirectories(rootDir);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: blue; font-weight: bold;">string</span> s <span style="color: blue; font-weight: bold;">in</span> dirs)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GetFiles(s);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">catch</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">Exception</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>If you run each versions of the code, you will see that this approach has most of the performance gains. It has better performance than ThreadPool approach since the IO thread always tries to keep the queue. Further, concentrate IO operations into a single thread helps the system optimize IO requests and thus increases the IO subsystem performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/09/05/producer-consumer-a-duplicate-file-finder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part IV</title>
		<link>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/</link>
		<comments>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 02:56:42 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/</guid>
		<description><![CDATA[In the previous three posts(I, II, III), I showed you how to inherit from GridView control to create an extended control that can bind to a generic list of objects. In this final post of the series, I will show you a simple example to illustrate how to use the GridViewEx control. 4. Putting it [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous three posts(<a href="/2008/08/08/extending-the-gridview-part-i/">I</a>,<a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/"> II</a>, <a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">III</a>), I showed you how to inherit from GridView control to create an extended control that can bind to a generic list of objects. In this final post of the series, I will show you a simple example to illustrate how to use the GridViewEx control.<span id="more-327"></span></p>
<p><strong>4. Putting it all together</strong></p>
<p>In fact, using an extended control isn&#8217;t that much different than using the stock control thanks to inheritance.&nbsp; For the most part, the extended control works exactly the same as the standard GridView control. I wanted to just touch base on a couple of places that a lot of people are having problem with.</p>
<p>First is the asp tag prefix issue. By default, all the custom controls (*.ascx) will automatically show up in the Toolbox strip within Vistuo Studio. So using the extended GridView is a simple matter of drag and drop. However, the default prefix for custom control is <em>cc1</em>, and depending on how many pre-existing custom controls there are on a page, it could be <em>cc{num}</em>. This becomes hard for replacing existing GridViews with the extended GridViewEx. To resolve this naming issue, we can register the assembly which contains the extended control within system.web-&gt;pages-&gt;controls tag in web.config:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">add</span><span style="color: blue;"> </span><span style="color: red;">assembly</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;"> </span><span style="color: red;">namespace</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;"> </span><span style="color: red;">tagPrefix</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;">/&gt;</span></div>
</div>
<p>With this prefix defined, we can use the tag ExtendedGridView wherever GridView is expected. By registering the prefix, we have also provided the solution to a second problem people often run into when using extended web controls&#8211;namely the ability to &quot;skin&quot; the control. With the extended control prefix registered, we can now create skins the same way as we do with other controls. For instance, my default.skin might look like:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">ExtendedGridView:GridViewEx</span><span style="color: blue;"> </span><span style="color: red;">runat</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">server</span>&quot;<span style="color: blue;"> </span><span style="color: red;">SkinID</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Professional</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Font-Name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Arial</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Font-Size</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">9pt</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Cellpadding</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">1</span>&quot;<span style="color: blue;"> </span><span style="color: red;">HeaderStyle-BackColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Gray</span>&quot;<span style="color: blue;"> </span><span style="color: red;">HeaderStyle-ForeColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">White</span>&quot;<span style="color: blue;"> </span><span style="color: red;">AlternatingRowStyle-BackColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">LightGray</span>&quot;<span style="color: blue;"> /&gt;</span></div>
</div>
<p>In this way, the theming issue of the extended GridView is addressed.</p>
<p>The code for the code behind page to display the extended GridView is trivial:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewPager1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>PagerSelectedIndexChanged;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> GridViewPager1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GridViewDataSourceChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(128, 128, 192); font-weight: bold;">!</span>Page<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>IsPostBack)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">SampleDataProvider</span> provider <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">SampleDataProvider</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> provider<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetSampleData();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> CBSelected_CheckedChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span> cbSel <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> sender <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (cbSel <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataControlFieldCell</span> cellObj <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cbSel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataControlFieldCell</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span> rowObj <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cellObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span> gv <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> rowObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> v <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; v[gv<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Index(rowObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowIndex)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cbSel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Checked;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> v;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>As you can see,&nbsp; the code is pretty much in line with what we would use for a typical GridView control. When the page is posted back, we set the DataSource and bind the data to the grid. Everything else happens transparently behind the scene. To illustrate the capability of GridViewEx, I included a CheckBox at the end of each row, whenever the check status is changed (CBSelected_CheckedChanged) we dynamically obtain the reference to the GridViewEx object (as an alternative, we could have used the actual control (GridViewEx1) instead of going through the trouble of dynamically converting if from the sender object since GridViewEx1 exists at compile time. Should the control be created dynamically, the code here will come in handy). Please pay special attention to how the checked state is determined. Because paging might be involved when the bound list has more entries than can be displayed, the current row index on the screen might not be the same as the row index in the list. To simplify coding, I created an Index function within GridViewEx to translate the selected row index into what is stored in the list.</p>
<p align="center"><img src="/blog/wp-content/uploads/2008/08/gridviewex.jpg" alt="" /></p>
<p>The code samples provided in this series can be downloaded <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">here</a>.</p>
<p>Of course, there are a lot more fancy things you can do with the grid or with any objects in that matter. The only limit is your imagination&#8230;</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part III</title>
		<link>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/</link>
		<comments>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 00:04:44 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/</guid>
		<description><![CDATA[In Part II, I showed you how to extend the GridView control so that we can bind a generic list to it. Because GridViewEx inherits from GridView, it has all the functionalities GridView has (i.e. paging). Sometimes, it is desirable to change the number of items displayed on a page dynamically, to accommodate this, we [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Part II</a>, I showed you how to extend the GridView control so that we can bind a generic list to it. Because GridViewEx inherits from GridView, it has all the functionalities GridView has (i.e. paging). Sometimes, it is desirable to change the number of items displayed on a page dynamically, to accommodate this, we will create a GridViewPager control (ascx).<span id="more-326"></span></p>
<p><strong>3. GridViewPager</strong></p>
<p>&nbsp;Here is the code for GridViewPager:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">partial</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewPager</span> : System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Web<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UI<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserControl</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">delegate</span> <span style="color: blue; font-weight: bold;">void</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">SelectedIndexChangedHandler</span>(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">int</span> pageSize);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">event</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">SelectedIndexChangedHandler</span> SelectedIndexChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">bool</span> _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">int</span> _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">10</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">bool</span> Visible</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[UniqueID] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>TryParse(ViewState[<span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UniqueID]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString(), <span style="color: blue; font-weight: bold;">out</span> _visible);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> _visible;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[UniqueID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> _visible;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (_visible)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MainPanel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MainPanel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> PageSize</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedValue);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(ViewState[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString());</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> _pageSize;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[<span style="color: rgb(43, 145, 175); font-weight: bold;">String</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnInit(<span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">EventHandler</span>(DropDownItemsPerPage_SelectedIndexChanged);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnInit(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> DropDownItemsPerPage_SelectedIndexChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedValue);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SelectedIndexChanged(sender, PageSize);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(128, 128, 192); font-weight: bold;">!</span>Page<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>IsPostBack)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> This method links to the DataSourceChangedEvent defined in GridViewEx so that</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> we can decide whether or not to show the pager depending on whether the list</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> to be bound is empty.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name=&quot;sender&quot;&gt;</span><span style="color: green;">sender object</span><span style="color: gray;">&lt;/param&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name=&quot;isEmpty&quot;&gt;</span><span style="color: green;">whether the list to be bound is empty</span><span style="color: gray;">&lt;/param&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> GridViewDataSourceChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">bool</span> isEmpty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (isEmpty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>As you can see, the pager control is quite simple. It communicates with GridViewEx control by the SelectedIndexChanged event. Depending on whether the state of number of items per page needs to persist between post backs, either ViewState or SessionState can be used.</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part II</title>
		<link>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/</link>
		<comments>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 00:10:53 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/</guid>
		<description><![CDATA[In Part I, I talked about why we needed to extend the GridView and some of the benefit in doing that. In this post, I will discuss how to extend the standard GridView control to take advantage of binding to the type safe value objects mentioned previously. 2. GridViewEx To make the binding as generic [...]]]></description>
			<content:encoded><![CDATA[<p>In<a href="/2008/08/08/extending-the-gridview-part-i/"> Part I</a>, I talked about why we needed to extend the GridView and some of the benefit in doing that. In this post, I will discuss how to extend the standard GridView control to take advantage of binding to the type safe value objects mentioned previously.<span id="more-325"></span></p>
<p><strong>2. GridViewEx</strong><br />
To make the binding as generic as possible, there are pretty much two ways. One way is to bind to an object collection of typed value objects. Since all value objects can be cast into objects, we could manipulate them generically. And because value objects are typically consisted of simple types, we could encapsulate all the operations needed for the GridView within the extended GridView itself. </p>
<p>Another solution is to utilize generics in .Net 2.0 and above. One obvious advantage is that the type information is not lost at any point of the object manipulation and thus a lot of small mistakes can be caught at compile time. This approach does have its draw backs. As you will see, because the purpose of generic types are to eliminate casting introduced by object containers, we have to change the extended GridView to accommodate the type of objects we are binding to the list whenever a new generic type is introduced. This of course requires the changing of the extended GridView control itself whenever we add a new generic type and thus might not be as convenient as the pure object oriented approach mentioned previously. However, weighing both the advantages and disadvantages I personally think this approach is more attractive. </p>
<p>So here, I will show you how to extend the GridView to handle generic lists.</p>
<p>GridViewEx inherits from GridView. We override the DataSource property so that we get a chance to inspect it before the databinding actually taking place. This gives us an opportunity to test whether the data source to be bound is empty. We use this information to decide whether to show the pager or not as you will see in a later post.</p>
<p>The initialization routine (OnInit) is overridden to setup some property values and hook up some events so that we provide row editing functionalities.</p>
<p>The RowEditing, PageSelectedIndexChanged and some of the other overridden functions are handled in a pretty standard way. To demonstrate how easy it is to add new functionality to the GridView, I added an event handler for RowDataBound. The purpose of the code for this RowDataBound event is to check if the CheckBox on a particular row is checked and if so, the background color of that row is changed. This behavior is particularly helpful for giving a visual cue to the end user which row of data he/she is currently dealing with. </p>
<p>When each row is created, OnRowCreated method is called, here we determine whether the row being created is the header or not and if so, we add an arrow to indicate the current sorting option depending on how the grid is currently sorted. If the row being created is a data row, we add some nice touch so that the data row becomes highlighted when mouse moves pass it.</p>
<p>We also have to override the OnSorting function to handle our generic lists. This is where we need to add the supported list types (see comments in code). For additional generic list types, we simply need to add three lines for each type of the list. While this might not be as convenient as using generic objects, all the changes are kept in a central place so that adding/removing types should not be an issue.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">namespace</span> ExtendedGridView</div>
<div style="margin: 0px;">{</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Extended GridView</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Supports paging/sorting using a customized object data source.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> http://www.kerrywong.com/</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span> : <span style="color: rgb(43, 145, 175); font-weight: bold;">GridView</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">delegate</span> <span style="color: blue; font-weight: bold;">void</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">DataSourceChangedEventHandler</span>(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">bool</span> isEmpty);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">event</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">DataSourceChangedEventHandler</span> DataSourceChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> A readonly unique ID for the current view.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> CurrentViewID</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentView_&quot;</span>, UniqueID); }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Current page id when paging is used.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> To remember the page index user selected, a session variable is used.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> CurrentPageID</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString());</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Overrides the data source property, so that we get a chance to test whether the source </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> being bound contains data or not.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> This is used to communicate with the pager so that for an empty list no pager is shown.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">object</span> DataSource</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: blue; font-weight: bold;">value</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSourceChanged(<span style="color: blue; font-weight: bold;">this</span>, <span style="color: blue; font-weight: bold;">true</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSourceChanged(<span style="color: blue; font-weight: bold;">this</span>, <span style="color: blue; font-weight: bold;">false</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Set up event handlers.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnInit(System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AllowPaging <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AllowSorting <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>AutoGenerateColumns <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">10</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowCancelingEdit <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewCancelEditEventHandler</span>(GridViewEx_RowCancelingEdit);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowDataBound <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewRowEventHandler</span>(GridViewEx_RowDataBound);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowEditing <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewEditEventHandler</span>(GridViewEx_RowEditing);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnInit(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnPageIndexChanging(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewPageEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>NewPageIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CurrentPageID <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> PageIndex;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Handles the sort event.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Since the data source is a generic list, we need to cast the list</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> back to what ever type it is before we can sort. All the list types</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> need to be handled here (see comments below)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnSorting(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewSortEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)(<span style="color: teal;">1</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> (<span style="color: blue; font-weight: bold;">int</span>)ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression]);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">object</span> o <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">null</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//This is where more list types can be added. </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Sort(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GenericComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>(e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection, e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression));</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; o <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> l;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnSorting(e);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> o;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> o;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnRowCreated(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRowEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnRowCreated(e);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowType <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowType</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Header)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">TableCell</span> cell <span style="color: blue; font-weight: bold;">in</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Cells)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>HasControls())</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">LinkButton</span> button <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls[<span style="color: teal;">0</span>] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">LinkButton</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (button <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Image</span> image <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Image</span>();</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: olive; font-weight: bold;">SortDirection</span> dir <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dir <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)ViewState[sortExpression];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> button<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CommandArgument)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (dir <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; image<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ImageUrl <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;~/images/sort_asc.gif&quot;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; image<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ImageUrl <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;~/images/sort_desc.gif&quot;</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(image);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span> <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowType <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowType</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataRow) <span style="color: green;">//Handles datarow highlighting on mouse move</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Attributes<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;onmouseover&quot;</span>, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=&#8217;lightgreen&#8217;&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Attributes<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;onmouseout&quot;</span>, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;this.style.backgroundColor=this.originalstyle;&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnDataBinding(System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (CurrentPageID <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> CurrentPageID;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnDataBinding(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowEditing(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEditEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EditIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>NewEditIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowDataBound(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRowEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">TableCell</span> cell <span style="color: blue; font-weight: bold;">in</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Cells)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Web<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UI<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">Control</span> c <span style="color: blue; font-weight: bold;">in</span> cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (c <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span> chkBox <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> c <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (chkBox<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Checked)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowState <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowState</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Alternate)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>BackColor <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175);">Color</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>LightSteelBlue;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>BackColor <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175);">Color</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>AliceBlue;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowCancelingEdit(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewCancelEditEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EditIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> PagerSelectedIndexChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">int</span> pageSize)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> pageSize;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Calculate the real index since the actual index returned from the </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> GridView is the index in relation to the current page. We need the</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> real index in order to index into the list.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> Index(<span style="color: blue; font-weight: bold;">int</span> rowIndex)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> rowIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">
<div>}</div>
</div>
</div>
<p>Because we are using customized objects as data bound objects, we also need to provider our own comparer so that the grid knows how to sort each column. This task is fairly easy and I modified the implementation from <a href="http://www.gridviewguy.com/ArticleDetails.aspx?articleID=203">GridViewGuy</a>. One thing you need to pay special attention is that since the comparison is done using property reflections, the property names are by default case sensitive (e.g. if you have a value property named <em>Item</em>, the binding in ASP.Net must be &quot;<em>Item</em>&quot; as well. &quot;item&quot; will give a run time error).&nbsp; The code for the comparer is as follows:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GenericComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>T<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>: <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>T<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: olive; font-weight: bold;">SortDirection</span> _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">string</span> _expression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: olive; font-weight: bold;">SortDirection</span> Direction</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _direction; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> GenericComparer(<span style="color: olive; font-weight: bold;">SortDirection</span> direction, <span style="color: blue; font-weight: bold;">string</span> expression)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> direction;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _expression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> expression;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> Compare(T x, T y)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">PropertyInfo</span> propertyInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">typeof</span>(T)<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetProperty(_expression);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span> obj1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span>)propertyInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetValue(x, <span style="color: blue; font-weight: bold;">null</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span> obj2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span>)propertyInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetValue(y, <span style="color: blue; font-weight: bold;">null</span>);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (_direction <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> obj1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CompareTo(obj2);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> obj2<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CompareTo(obj1);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView &#8212; Part I</title>
		<link>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/</link>
		<comments>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 00:41:55 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/</guid>
		<description><![CDATA[Most business applications require presenting result sets to the end users in the form of some kind of grid views. While the default ASP.NET GridView control is sufficient for simple scenarios when doing rapid application development, its drawbacks become clear when building any large scale solutions (discussions here apply to all the current .Net frameworks, [...]]]></description>
			<content:encoded><![CDATA[<p>Most business applications require presenting result sets to the end users in the form of some kind of grid views.<span id="more-323"></span> While the default ASP.NET GridView control is sufficient for simple scenarios when doing <a href="http://en.wikipedia.org/wiki/Rapid_application_development">rapid application development</a>, its drawbacks become clear when building any large scale solutions (discussions here apply to all the current .Net frameworks, 2.0, 3.5 etc.).</p>
<p>For instance, the default data binding is limited to SqlDataSource, ObjectDataSource or a DataSet object (including DataView, DataTable, etc.). When building a large scale business solution, it is generally desirable to complete separate the data manipulation and business logic from the presentation layer and the most common solution is to employee a <a href="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</a> pattern.&nbsp; This requirement makes SqlDataSource and DataSet bindings unsuitable. While ObjectDataSource does provide some level of isolation, the way business objects can be constructed is limited in order for these objects to be used as ObjectDataSource.</p>
<p>Most business objects incorporate the so-called <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object">Value Object</a> data structure. Basically, it is a serializable class with the sole purpose of holding values and possibly other functionalities like data validation. Unfortunately, by default, GridView&#8217;s support for such value objects is poor (i.e. you can bind a value object to a grid view, but sorting is not supported by default). Over the years, I have seen many ad-hoc implementations to make GridView suitable for binding with customized value objects. Unfortunately, many of the solutions were implemented poorly and the implementation techniques vary from person to person, making it difficult if not impossible to maintain.</p>
<p>In the next few blog posts, I will show you how to extend the stock GridView in ASP.Net to make it suitable for using with value objects. The goal of extending the standard GridView is not only to add sorting capabilities, but to make the new component compatible with the standard GridView. I also intended to hide as much implementation details as possible so that the end users do not need to worry about the syntactical nuances as do with the standard GridView when binding to objects.</p>
<p>I assume that the development environment is Visual Studio 2005 with AJAX extension installed. The samples I show here can run within either Visual Studio 2005 (with AJAX extension) or Visual Studio 2008 without any modifications.</p>
<p><strong>1.Defining the Value Object </strong></p>
<p>As mentioned before, the reason we would like to define our own so-called value objects instead of using a DataSet to hold data is to ensure type safety. Since DataSet can hold any kind of objects, debugging becomes more difficult as error checking happens at run time not compile time. It also makes it easy to make mistakes as columns must be accessed by their names and any changes in column names could result in code broken at runtime somewhere in the UI.&nbsp; Most importantly, binding a dataset directly to the UI violates the layered design principal, making the data and the presentation tightly coupled and difficult to adapt. The solution is to create a level of indirection by utilizing the so called value objects.  Value objects are nothing fancy, they are merely a serializable class that provides the properties that represent the data (e.g. a row in a table) we are dealing with. For instance, here is what we might create as a value object for a simple phone book:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175); font-weight: bold;">Serializable</span>()]</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> _selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> Name</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _name; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> Address</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _address; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> ZipCode</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _zipCode; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> PhoneNumber</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _phoneNumber; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">bool</span> Selected</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _selected; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> ItemVal(<span style="color: blue; font-weight: bold;">string</span> name, <span style="color: blue; font-weight: bold;">string</span> address, <span style="color: blue; font-weight: bold;">string</span> zipCode, <span style="color: blue; font-weight: bold;">string</span> phoneNumber)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> name;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> address;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> zipCode;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> phoneNumber;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>By creating a collection of such value object we just defined, we have a full representation of the result set returned from the database via a query or stored procedure. And via value objects, all the accesses are guaranteed to be type safe.</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Line of Code Could Bring Visual Studio .Net Down to Its Knees</title>
		<link>http://www.kerrywong.com/2008/06/06/a-line-of-code-could-bring-visual-studio-net-down-to-its-knees/</link>
		<comments>http://www.kerrywong.com/2008/06/06/a-line-of-code-could-bring-visual-studio-net-down-to-its-knees/#comments</comments>
		<pubDate>Sat, 07 Jun 2008 01:41:38 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/06/06/a-line-of-code-could-bring-visual-studio-net-down-to-its-knees/</guid>
		<description><![CDATA[It seems at least to me that as more bells and whistles were added to each new release of Microsoft&#8217;s products, the qualities of which have been deteriorating. My observation not only manifested itself in the public perception of Windows Vista, but also became self-evident in my and many of my peers&#8217; daily frustration with [...]]]></description>
			<content:encoded><![CDATA[<p>It seems at least to me that as more bells and whistles were added to each new release of Microsoft&#8217;s products, the qualities of which have been deteriorating.<span id="more-300"></span> My observation not only manifested itself in the <a href="http://crave.cnet.co.uk/gadgets/0,39029552,49293700-10,00.htm">public perception</a> of Windows Vista, but also became self-evident in my and many of my peers&#8217; <a href="http://forums.asp.net/p/1232164/2226940.aspx">daily frustration</a> with Microsoft&#8217;s flag-ship IDE &#8211; Visual Studio .Net.</p>
<p>Recently, while working on some projects, I accidentally found out how easy it is to crash the Visual Studio .Net IDE and all it takes is literately one line of code.</p>
<p>Apparently, it seems that some developers at Microsoft must have forgotten the old design principal in software engineering: Never assume that a user won&#8217;t do certain things. From the surface Visual studio 2005 and 2008 look much fancier than their old cousin Visual Studio .Net 2003.&nbsp; But neither of them can&nbsp; handle the following line of code correctly without crashing:</p>
<blockquote style="margin-right: 0px;" dir="ltr">
<p><font face="Courier New">s&nbsp;= s.Trim().Trim()&#8230;.. Trim();&nbsp; <font color="#339966">//will crash if the line length is too long</font></font></p>
</blockquote>
<p>If you try to move the cursor to the end of the line in either VB or C#, the IDE will crash (here is the <a href="/blog/wp-content/uploads/2008/06/vscrash.zip">sample code</a> for VB.Net and C#). And it seems that there&#8217;s little consistency between the different .Net language compilers (vbc and csc) in terms of what is the maximum line length it can handle. It seems that for C#, the IDE can handle line width (in terms of the code above) of roughly 20,000 before crashing while VB.Net could not handle even half of that. It is also interesting that the code line length limitation seemed to be more restrictive in Visual Studio 2008 than in Visual Studio 2005 (<em>note, it could as well be the OS limitation since in my experiment, the Visual Studio 2008 instance was running under Windows Vista whereas the Visual Studio 2005 instance was running under Windows Server 2003</em>).</p>
<p>Also, the error messages you received when running the same program within VS 2005 and VS2008 were different as well. For instance, if you are compiling the sample programs under Visual Studio 2008, the C# program gives a pretty accurate error message:</p>
<blockquote>
<p><strong>An expression is too long or complex to compile near &#8221;</strong></p>
</blockquote>
<p>But the VB program could not be compiled at all, and you will get an internal error from the VB compiler:</p>
<blockquote>
<div><strong>The &quot;Vbc&quot; task failed unexpectedly.<br />
System.Runtime.InteropServices.SEHException: External component has thrown an exception.<br />
&nbsp;&nbsp; at Microsoft.Build.Tasks.Hosting.IVbcHostObject.EndInitialization()<br />
&nbsp;&nbsp; at Microsoft.Build.Tasks.Vbc.InitializeHostCompiler(IVbcHostObject vbcHostObject)<br />
&nbsp;&nbsp; at Microsoft.Build.Tasks.Vbc.InitializeHostObject()<br />
&nbsp;&nbsp; at Microsoft.Build.Utilities.ToolTask.Execute()<br />
&nbsp;&nbsp; at Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean&amp; taskResult)&nbsp;&nbsp;</strong></div>
</blockquote>
<p>Sometimes, you will get this error when trying to compile the VB program:</p>
<p align="center"><img alt="" src="/blog/wp-content/uploads/2008/06/vb_vs2008.jpg" /></p>
<p>And here are the corresponding application events in event viewer (corresponding to the VB environment crash):</p>
<p align="center"><img alt="" src="/blog/wp-content/uploads/2008/06/vb_vs2008_eventviewer.jpg" /></p>
<p align="center"><img alt="" src="/blog/wp-content/uploads/2008/06/vb_vs2008_fault.jpg" /></p>
<p>When trying to move to the end of the line, both VB and C# application would crash the IDE and give the following error:</p>
<p align="center">&nbsp;<img alt="" src="/blog/wp-content/uploads/2008/06/vs_vs2008.jpg" /></p>
<p align="left">And the Visual Studio 2008 IDE would then close.</p>
<p>Within Visual Studio .Net 2005, if you try to move the cursor to the end of the line in the C# version, the application simply closes (crashed) but no error message is given. But if you try to compile the C# code, you actually get the following error indicating that the line is too long:</p>
<blockquote>
<p><strong>An expression is too long or complex to compile near &#8216;VSCrash.Program.Main(string[])&#8217;&nbsp;&nbsp; </strong></p>
</blockquote>
<p>The same code in VB would not even load without giving the following error:</p>
<p align="center"><img alt="" src="/blog/wp-content/uploads/2008/06/vb_vs2005_load.JPG" /></p>
<p align="left">And if you close the error dialog and try to compile the VB.Net code, you get this error message:</p>
<blockquote>
<div align="left"><strong>Visual Basic compiler is unable to recover from the following error:<br />
System Error &amp;Hc00000fd&amp;<br />
Save your work and restart Visual Studio.</strong></div>
</blockquote>
<p align="left">Which is the same error message as in Visual Studio 2008. And the event viewer records the following information:</p>
<p align="center"><img alt="" src="/blog/wp-content/uploads/2008/06/vb_vs2005_appevent.JPG" /></p>
<p>&nbsp;</p>
<p>The VB.Net environment&#8217;s crashes are more troublesome. As indicated by the event viewer, it seems that the the exception chain went all the way to the kernel32.dll (note, if you choose the debug option when the VS 2005/2008 environment crashes, you can actually see the full exception chain along the execution path).</p>
<p>In my opinion it is one thing that the IDE has some design or practical limitations and it is totally another if the IDE simply crashes. Alas,<br />
the only IDE that can handle this code correctly is Visual Studio .Net 2003.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/06/06/a-line-of-code-could-bring-visual-studio-net-down-to-its-knees/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Event Logging Using Reflection</title>
		<link>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/</link>
		<comments>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/#comments</comments>
		<pubDate>Mon, 26 May 2008 01:47:46 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/</guid>
		<description><![CDATA[When exceptions occur during the execution of&#160;an application, the stack trace contains the detailed location of where the exception is originated.&#160;And for a properly designed application (e.g. all exceptions are properly propagated), this information is adequate. However, some applications need the logging of information not only when exception occurs but along its critical execution path [...]]]></description>
			<content:encoded><![CDATA[<p>When exceptions occur during the execution of&nbsp;an application, the stack trace contains the detailed location of where the exception is originated.<span id="more-290"></span>&nbsp;And for a properly designed application (e.g. all exceptions are properly propagated), this information is adequate. However, some applications need the logging of information not only when exception occurs but along its critical execution path as well. As a result, I see a lot of programs having the location information (for example class names and function names, etc.) hard coded where logging is taken place. This approach, however, tends to introduce inconsistencies over time as the application&nbsp;is modified by many different developers and sometimes the changes to function or class names are not in sync with what is being logged.</p>
<p>This problem can be easily circumvented by using reflections. The following code snippet demonstrates how the current function&#8217;s fully qualified name can be obtained using reflection:</p>
<p><font size="2"></p>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Type</span> t <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: blue;">this</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetType();</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> aName <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> t<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>FullName;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> mName <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: rgb(43, 145, 175);">MethodBase</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetCurrentMethod()<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Name;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Console</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>WriteLine(<span style="font-weight: bold; color: blue;">string</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Format(<span style="font-weight: bold; color: rgb(163, 21, 21);">&quot;{0}.{1}&quot;</span>, aName, mName));</div>
<p><!--EndFragment--></font></p>
<p>And reflection can also be used to obtain the values of member fields (e.g. public and private, etc.):</p>
<p><font size="2"></p>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Type</span> t <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: blue;">this</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetType();</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">FieldInfo</span>[] fields <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> t<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetFields(<span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Instance <span style="font-weight: bold; color: rgb(128, 128, 192);">|</span> <span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>NonPublic <span style="font-weight: bold; color: rgb(128, 128, 192);">|</span> <span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Public);</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">foreach</span> (<span style="font-weight: bold; color: rgb(43, 145, 175);">FieldInfo</span> field <span style="font-weight: bold; color: blue;">in</span> fields)</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> fieldValue <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: rgb(43, 145, 175);">String</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Concat(field<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Name, <span style="font-weight: bold; color: rgb(163, 21, 21);">&quot;: &quot;</span>, field<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetValue(<span style="font-weight: bold; color: blue;">this</span>)<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>ToString());</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Console</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>WriteLine(fieldValue);</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<p><!--EndFragment--></font></p>
<p><!--EndFragment--></p>
<p>This is particularly helpful when logging exceptions, as the member variables&#8217; states would be preserved and it might provide valuable information in determining the causes of the exceptions.</p>
<p>We need to be careful where we use the techniques mentioned above however, as&nbsp;reflection typically comes with pretty hefty performance penalties and thus must be used sparsely where logging is absolutely needed. When reflection is used within the exception catching block, performance is usually not a concern. But we must pay attention to ensure not to cause further exceptions within the exception handling code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Could not Find File &#8216;Microsoft.Windows.CommonLanguageRuntime&#8217; Error</title>
		<link>http://www.kerrywong.com/2008/05/17/could-not-find-file-microsoftwindowscommonlanguageruntime-error/</link>
		<comments>http://www.kerrywong.com/2008/05/17/could-not-find-file-microsoftwindowscommonlanguageruntime-error/#comments</comments>
		<pubDate>Sun, 18 May 2008 01:06:03 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[CLR]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/05/17/could-not-find-file-microsoftwindowscommonlanguageruntime-error/</guid>
		<description><![CDATA[I ran into this weird compile time error the other day, and as it turned out, this is due to the limitation of Visual Studio 2005 deployment capabilities.&#160; &#160;A Google search on that error would turn out many hits. The official explanation can be found in this Microsoft knowledge base article. In my case, I [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into this weird compile time error the other day, and as it turned out, this is due to the limitation of Visual Studio 2005 deployment capabilities.&nbsp;<span id="more-289"></span></p>
<p>&nbsp;A Google search on that error would turn out many hits. The official explanation can be found in this <a href="http://support.microsoft.com/kb/907757">Microsoft knowledge base article</a>. In my case, I have a solution file that contains multiple projects. Among them, one is a web project and one is a console application project. And the web project has a reference to the console application since it shares some functionality with the console app.</p>
<p>This setting has been working well until I added a web deployment project. After adding the web deployment project, I would get</p>
<blockquote dir="ltr" style="margin-right: 0px">
<p><em>Could not Find File &#8216;Microsoft.Windows.CommonLanguageRuntime, Version=2.0.50727.0</em></p>
</blockquote>
<p>error at compile time. The easiest way to get around this limitation is to change the console application into a class library and create another project (in this case a console application) that simply instantiate the class&nbsp;and cal the original console app&#8217;s corresponding function. Using this technique, the web application no longer references to the console application but to a class library and thus the above compile time error would not occur.</p>
<p>After you fix this error, you might get a &quot;could not find certificate error&quot; when you try to compile. This is because when the deployment project was built the very first time (when the web project had a reference to the console application), the deployment was trying to build the console application portion as a ClickOnce application. If you look at the project settings (Project-&gt;Properties) for the console app (or class library if you had changed it to a class library project), you will see that the &quot;Sign the ClickOnce manifests&quot; option is checked. Uncheck this option will eliminate the certificate error. The check box might be checked but grayed out (at least this was what I saw), and if this is the case, you will need to manually remove the configuration from the .proj file (search for ClickOnce).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/05/17/could-not-find-file-microsoftwindowscommonlanguageruntime-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unexpected Event Behavior</title>
		<link>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/</link>
		<comments>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/#comments</comments>
		<pubDate>Sun, 04 May 2008 01:13:55 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/</guid>
		<description><![CDATA[Typically, there is rarely a need to change a control&#8217;s ID. However, this practice is sometimes useful when debugging a custom control, partiularly one that contains dynamically generated controls. When a control resides&#160;within an&#160;AJAX update panel which in turn resides in a Content control on a page that uses a master page however, changing the [...]]]></description>
			<content:encoded><![CDATA[<p>Typically, there is rarely a need to change a control&#8217;s ID. However, this practice is sometimes useful when debugging a custom control, partiularly one that contains dynamically generated controls.<span id="more-287"></span></p>
<p>When a control resides&nbsp;within an&nbsp;AJAX update panel which in turn resides in a Content control on a page that uses a master page however, changing the control ID will result in the&nbsp;attached event not being fired (please note, if the update panel is placed in a page directly, e.g. no master page is used, the code iluustrated below works correctly). At least this is what I have observed&nbsp;in&nbsp;AJAX extension 1.0 for Visual Studio&nbsp;2005.&nbsp;</p>
<p>The the following simple user control for example&nbsp;&nbsp;</p>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New"><span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: blue">partial</span> <span style="font-weight: bold; color: blue">class</span> <span style="font-weight: bold; color: #2b91af">WebUserControl</span> : System.Web.UI.<span style="font-weight: bold; color: #2b91af">UserControl</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">{</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">protected</span> <span style="font-weight: bold; color: blue">void</span> Page_Load(<span style="font-weight: bold; color: blue">object</span> sender, <span style="font-weight: bold; color: #2b91af">EventArgs</span> e)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; BuildControls();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">private</span> <span style="font-weight: bold; color: blue">void</span> BuildControls()</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">CheckBox</span> cb = <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">CheckBox</span>();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cb.AutoPostBack = <span style="font-weight: bold; color: blue">true</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">//Event will not fire if the control&#8217;s ID is changed</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">//and the control is in an update panel.</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <font style="background-color: #ffff00">cb.ID = <span style="font-weight: bold; color: #2b91af">String</span>.Format(<span style="font-weight: bold; color: #a31515">&quot;{0}{1}&quot;</span>, <span style="font-weight: bold; color: #a31515">&quot;MyBtn&quot;</span>, UniqueID); </font></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cb.CheckedChanged += <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #8080c0">EventHandler</span>(cb_CheckedChanged);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span>.Controls.Add(cb);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">void</span> cb_CheckedChanged(<span style="font-weight: bold; color: blue">object</span> sender, <span style="font-weight: bold; color: #2b91af">EventArgs</span> e)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Label1.Text = <span style="font-weight: bold; color: #2b91af">String</span>.Format(<span style="font-weight: bold; color: #a31515">&quot;Check box checked: {0} &lt;br /&gt;&quot;</span>, (sender <span style="font-weight: bold; color: blue">as</span> <span style="font-weight: bold; color: #2b91af">CheckBox</span>).Checked);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">}</div>
<p><!--EndFragment--></p>
<p>When the above control (a dynamic button and a label) is embedded within a normal page (e.g. a non-AJAX page), the CheckedChanged event is fired whenever the check box is clicked. But when the same control is put into an update panel, whith the highlighted line of code included, the event will no longer fire.</p>
<p>This strange behavior had caused me some headaches while debugging through a rather complex web user control. So you will need to be extra careful when controls are placed in update panels. Due to limitations like the one I just mentioned a control that works in a normal page might not work properly in an AJAX enabled web page. The solution to this problem is simple, just remove the highlighted line and the event will fire again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Strange Databinding Issue</title>
		<link>http://www.kerrywong.com/2008/03/08/a-strange-databinding-issue/</link>
		<comments>http://www.kerrywong.com/2008/03/08/a-strange-databinding-issue/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 03:42:53 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/03/08/a-strange-databinding-issue/</guid>
		<description><![CDATA[It seems that the DropDownList control in ASP.Net 2.0 really has some issues. Besides the one I mentioned in an earlier post, I recently ran into another annoying problem. The symptom: while using a custom object data source (e.g. as simple as a value object, I have not tested other data sources yet, but a [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that the DropDownList control in ASP.Net 2.0 really has some issues. Besides the one I mentioned in <a href="/2007/12/21/interesting-databinding-behavior/">an earlier post</a>, I recently ran into another annoying problem.<span id="more-270"></span></p>
<p><strong>The symptom</strong>: while using a custom object data source (e.g. as simple as a value object, <em>I have not tested other data sources yet, but a quick search on this issue on the Internet seemed to suggest that the issue I will be talking here happens with other data sources as well</em>), it seems that changing SelectedValue after the control is bound would sometimes trigger the infamous <em><font color="#800000">&lsquo;DropDownList1&prime; has a SelectedValue which is invalid because it does not exist in the list of items.</font> </em>exception.</p>
<p>This problem is rather frustrating as it does not always happen. It seems to me though that whenever the data source has non-consecutive values bound to SelectedValue, this exception will occur if trying to change the SelectedValue property after the data has already been bound.</p>
<p>After some experiment, I think I found a workaround. While changing SelectedValue triggers the exception, changing SelectedIndex does not. So, if the selected value of a dropdown list must be changed after data bound, change the SelectedIndex instead.</p>
<p>This workaround of course, adds a little bit complexity to the code as you must use some routine to determine the index of th value that is being selected. In most cases though, the added code is no more than a for loop that inspects the data-bound object.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/03/08/a-strange-databinding-issue/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reflection and Case Sensitivity</title>
		<link>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</link>
		<comments>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 02:48:56 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</guid>
		<description><![CDATA[Reflection is case sensitive by default, which comes at no surprise to most C# developers.However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do [...]]]></description>
			<content:encoded><![CDATA[<p>Reflection is case sensitive by default, which comes at no surprise to most C# developers.<span id="more-263"></span>However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do with the case sensitivity issue while using reflection.</p>
<p>I was doing some ASP.NET work and was using customized objects as data sources to bind to DataGrid. For sorting, I used the technique similar to what described in <a href="http://www.gridviewguy.com/ArticleDetails.aspx?articleID=203">Sorting GridView Using IComparer</a>. When I was testing my grids, however, I noticed that for most of the columns it worked perfectly, but for a few I somehow was greeted with the null reference error.</p>
<p>At first I thought that this was caused by the null items in those columns. After spending quite sometime digging, I realized that this was due to the case sensitivity issue in reflection. I had used the wrong case in the DataField of&nbsp; the BoundField. And after I changed the spelling to match that defined in the value object, the problem was solved.</p>
<p>VB developers should pay special attention when using reflections, as in VB.Net the language itself is not case sensitive.</p>
<p>As a side note, even though it is possible to set the binding flag to make the member lookup case insensitive, it is not recommended as <a href="http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx?loc=&amp;side=true">such approach tend to make the operation less efficient</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enable/Disable Controls using ControlCollection</title>
		<link>http://www.kerrywong.com/2008/01/25/enabledisable-controls-using-controlcollection/</link>
		<comments>http://www.kerrywong.com/2008/01/25/enabledisable-controls-using-controlcollection/#comments</comments>
		<pubDate>Sat, 26 Jan 2008 02:00:58 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/01/25/enabledisable-controls-using-controlcollection/</guid>
		<description><![CDATA[In a role based application, it is often required that certain controls on the UI form be enabled and disabled based on user&#8217;s security credentials.And the easiest way to implement such functionality in a web application is to utilize the ControlCollection that is accessible on every page. Like their WinForm counterparts, controls on a webform [...]]]></description>
			<content:encoded><![CDATA[<p>In a role based application, it is often required that certain controls on the UI form be enabled and disabled based on user&#8217;s security credentials.<span id="more-262"></span>And the easiest way to implement such functionality in a web application is to utilize the ControlCollection that is accessible on every page.</p>
<p>Like their WinForm counterparts, controls on a webform can be and often is hierarchical and thus requires recursion to traverse all the controls. But unlike WinForm controls, web controls do not uniformly share the <em>Enabled </em>property. This means that we would have to cast each control back to their own types before we could set their <em>Enabled </em>property. But in reality, this should not be a big concern as there are only a few primitive types of controls that are editable.</p>
<p>The following code snippet shows how to achieve this in C# (here we set the TextBox and Button controls to be disabled):&nbsp;</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetControlStatus(Controls, <span style="color: blue; font-weight: bold;">false</span>); <span style="color: green;">//disabled</span></div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> SetControlStatus(<span style="color: rgb(43, 145, 175); font-weight: bold;">ControlCollection</span> col, <span style="color: blue; font-weight: bold;">bool</span> enabled)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">Control</span> c <span style="color: blue; font-weight: bold;">in</span> col)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ChangeContolStatus(c, enabled);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetControlStatus(c<span style="color: navy;">.</span>Controls, enabled);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">void</span> ChangeContolStatus(<span style="color: rgb(43, 145, 175); font-weight: bold;">Control</span> c, <span style="color: blue; font-weight: bold;">bool</span> enabled)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (c <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TextBox</span>)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (c <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">TextBox</span>)<span style="color: navy;">.</span>Enabled <span style="color: navy;">=</span> enabled;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span> <span style="color: blue; font-weight: bold;">if</span> (c <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Button</span>)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (c <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Button</span>)<span style="color: navy;">.</span>Enabled <span style="color: navy;">=</span> enabled;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>In ASP.Net 2.0 and above, this code can be placed within the MasterPage and thus makes enabling and disabling controls at runtime even more trivial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/01/25/enabledisable-controls-using-controlcollection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Elusive File.Copy</title>
		<link>http://www.kerrywong.com/2008/01/11/the-elusive-filecopy/</link>
		<comments>http://www.kerrywong.com/2008/01/11/the-elusive-filecopy/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 02:40:25 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/01/11/the-elusive-filecopy/</guid>
		<description><![CDATA[A while ago, I discussed some interesting behavior of File.Exist function in .Net. Recently I found out that File.Copy is just as elusive when UNC is involved. There are many articles out there discussing problems developers have encountered with File.Copy. While most of those problems are user problems, a few of those problems were simply [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I discussed some <a href="/2007/06/12/fileexists-and-unc/">interesting behavior</a> of File.Exist function in .Net. Recently I found out that File.Copy is just as elusive when UNC is involved.<span id="more-259"></span></p>
<p>There are many articles out there discussing problems developers have encountered with File.Copy. While most of those problems are user problems, a few of those problems were simply mysterious to say the least. Here is the problem I encountered recently:</p>
<p>I have written a C# application that moves files from one NAS to local machine. The user under which the application is launched belongs to an AD group which has Administrative rights on both locations. The strange thing is that once a while (for example, one out of a hundred files) I would receive an Access denied exception saying that I do not have the rights to copy a particular file from the UNC location. With the same user credential as the the application however, I could manually copy the problematic file without any issues.</p>
<p>At first, I thought that it might have just been an transient error, but multiple tries would error out on the exact same file (all together, there were hundreds of files). Interestingly enough, this file has exactly the same permissions as other files that were successfully copied. Even though this file is a bit larger than the other files, my applications had successfully copied many larger files before&#8230;</p>
<p>One <a href="http://www.mcse.ms/message1536741.html">discussion thread</a> I found suggested that it might be a delegation issue. But given that the application was able to move many other files except the once-awhile hitch-ups, I doubt that it was the permission issue.</p>
<p>The only thing I can think of is that the copy might simply have encountered a problem (e.g. disk problem, maybe), but somehow the wrong error message got returned. While, I wouldn&#8217;t be surprised if this is indeed the case, as I have encountered similar problems <a href="http://2007/10/23/lousy-gdi-error-messages/">before</a>.</p>
<p>Anyway, I will see if I can figure this out&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/01/11/the-elusive-filecopy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
