Download The ColumnDragdataGrid.msi File
Introduction
A few months ago, when I was getting started at Microsoft, my manager walked into my office, and detailed a project on which I would be spending the next two weeks. I was to conjure up an application that would be used to aggregate metrics for content strategists at MSDN. One of the feature requests was a DataGrid-like control that would allow users to arrange columns in their preferred order before exporting the data to a Microsoft Excel spreadsheet. His last words prior to departing my office were, "Make it AN Interesting User Experience. "
I knew that in order to be able to rearrange DataGrid columns, I had to manipulate the DataGrid's DataGridColumnStyle property to reflect the new column ordering, but that was not exactly enthralling. I wanted a visual representation of the whole dragging operation. I started out playing around with some System.Drawing functionality, and got to a point where I was able to drag shapes across the screen. I decided that I needed to kick it up a notch. Instead of just dragging a bland and uninspiring rectangle painted atop the DataGrid drawing surface, I could make it appear as if the user was dragging the column. I dug down to the roots of the native GDI library and after several hours of experimenting, figured out what I needed to do in order to achieve this trickery.
Figure 1. The Dragging Operation
Getting start
The first thing I needed to do was figure out how I was going to take a screenshot of the column that was about to be dragged. I knew exactly what I needed and wanted to do, but did not know how to do it. After discovering that the classes residing under the System.Drawing namespace did not provide me with the functionality that I needed to perform screen captures, I looked into the native GDI library and found that the BitBlt function was exactly what I was looking for.The next Step Was to Write a Managed Wrapper Around this function. The First Thing I'll Cover In This Article Is How I Implement The ScreenImage Class.
The ScreenImage Class
In order to make invocations across the interoperation boundary, we need to declare the unmanaged functions and indicate which libraries they come from so the JIT compiler knows where to find them during runtime. Once this has been done, all we have to do is invoke them Just As We do with Managed Method Invocations, As Seen In The Code Block Below.
Public Sealed Class ScreenImage {
[DLLIMPORT ("gdi32.dll")]]]]]]
Private Static Extern Bool Bitblt (INTPTR
HandlertodestinationDeviceContext, int x, int y, int nwidth, int nheight,
INTPTR HANDLERTOSODEVICECECONTEXT, INT XSRC, INT YSRC, INT OPCODE
[DLLIMPORT ("User32.dll")]]]]]
Private static extern INTPTR getWindowdc (INTPTR windowHandle);
[DLLIMPORT ("User32.dll")]]]]]
Private Static Extern Int ReleaseDC (INTPTR WINDOWHANDLE, INTPTR DC);
Private static int srcopy = 0x00cc0020;
Public Static Image GetScreenshot (INTPTR WINDOWHANDLE, POINT LOCATION, SIZE SIZE) {
...}
}
This class only exposes one method, GetScreenshot, which is a static method that returns an image object containing color data corresponding to the windowHandle, location, and size parameters. The next code block shows how I implemented this method.public static Image GetScreenshot (IntPtr WindowHandle, Point Location, Size Size {
Image myimage = new bitmap (size.width, size.Height);
USING (Graphics g = graphics.fromimage (myimage)) {
INTPTR DESTDEVICECONTEXT = g.getHDC ();
INTPTR SRCDEVICECONTEXT = getWindowdc (WindowHandle);
// Todo: throw Exception
Bitblt (destDeviceContext, 0, 0, size.width, size.height,
SrcDeviceContext, Location.x, Location.y, SRCCopy;
ReleaseDC (WindowHandle, SrcDeviceContext);
G.ReleaseHDC (DestDeviceContext);
} // dispose the graphics object
Return myimage;
}
Let's take a line-by-line look at the method IMplementation. The First Thing i Did Was Create a New Bitmap with Dimensions Corresponding to the parameterized size.
Image myimage = new bitmap (size.width, size.Height);
The Following Line of Code Retrieves The Drawing Surface Associated with The New Bitmap That Was Just Created.
USING (Graphics G = graphics.fromimage (myimage)) {...}
The C # using keyword defines a scope at the end of which the Graphics object will be disposed. Since all of the classes in the System.Drawing namespace are managed wrappers around the native GDI API, we are almost always dealing with unmanaged resources, so we need to ensure that we jettison resources whose services are no longer needed. This process is called deterministic finalization and allows for the resources that are used by the object to be reallocated for other purposes immediately, rather than waiting for the garbage collector to come around and do its job. This is a practice that should be adhered to whenever you're dealing with objects that implement the IDisposable interface, such as the Graphics object used here.Handles to the source and destination device contexts are retrieved so we can proceed with the THEFER OF THE COLOR DATA. The Source Is The Device Context Associated with The Parameterized WindowHandle Handle and The Destination Is The Device Context from the Bi TMAP CREATED EARLIER.
INTPTR SRCDEVICECONTEXT = getWindowdc (WindowHandle);
INTPTR DESTDEVICECONTEXT = g.getHDC ();
Tip A device context is a GDI data structure that is maintained internally by Windows and defines a set of graphical objects, as well as the graphical modes that affect output relating to those objects. Think of it as a canvas that Windows gives you on which to .
Now we have a defined Bitmap object (myImage) and a device context representing the canvas of this object, which at this point of time in execution is transparent. The native BitBlt method requires the coordinates and the size of the portion of the canvas that we want to copy the bits to and the coordinates on the source device context from where we want to start copying bits. The method also requires a raster operation code value to define how the bit blocks are to be transferred.Here, I set the starting coordinates of the destination device context to the top left corner and the raster operation code value to SRCCOPY, which signifies that you want to copy the source directly to the destination. The hexadecimal value equivalent (00x00CC0020) was retrieved from the GDI header file.
Bitblt (destDeviceContext, 0, 0, size.width, size.height,
SrcDeviceContext, Location.x, Location.y, SRCCopy;
Once we're done with the device contexts, we need to release them. Failure to do so will result in the device context not being available for subsequent requests and possibly cause runtime exceptions to be thrown.
ReleaseDeviceContext (WindowHandle, DestdeviceContext);
G.ReleaseHDC (SrcDeviceContext);
I affirmed that the ScreenImage class was working as expected, then the next thing I needed to do was create a simple data structure that would help me keep track of all of the data related to the column that was being dragged.
The DraggedDataGridColumn Class
The DraggedDataGridColumn class is a data structure that monitors the various states of the dragged column, including the initial location, current location, image representation, and cursor location relative to the column's initial origin. For a detailed description of all of the parameters, please take a look at the code in draggeddatagridcolumn.cs.tip if you class encapsulates an Object That IMPLEMENTS
Idisposable, you might be indirectly Holding Onto Unmanaged Resources. In this case, your class shouth
Idisposable interface and invoke the
Dispose () Method on Each Disposable Object. The
DraggedDataGridColumn Class Encapsulates A
Bitmap Object, Which Explicitly Holds Onto Unmanaged Resources, So It Was Imperative That i Completed this STEP.
With this out of the way
The ColumnDragdataGrid Class
The DataGrid control is a powerful heavyweight control, but it does not natively provide us with the ability to drag-and-drop columns, so I had to extend it and add that functionality myself. Three different mouse events were handled and the DataGrid's OnPaint Method Was Overridden to Fulfill All of My Drawing Needs.
First, Let's Take a Look at All of the Member Fields That Are Used To Keep Track of Where And How Things Should Be Drawn.
Member FieldsDefinitionsm_initialRegionA DraggedDataGridColumn object that represents everything about the column that is currently being dragged. I'll get into the specifics of the DraggedDataGridColumn class later in this article.m_mouseOverColumnRectA Rectangle structure that is used to identify the rectangular region representing the column above which the mouse cursor is currently hovering.m_mouseOverColumnIndexThe index of the column that the mouse cursor is currently over.m_columnImageA Bitmap object containing a bitmap representation of the column at the time that the drag-and-drop operation was initiated.m_showColumnWhileDraggingA Boolean value representing whether the captured column image should be shown when the column is being dragged. This is exposed publicly through the ShowColumnWhileDragging property.m_showColumnHeaderWhileDraggingA Boolean value representing whether the header portion of the column should be shown when the column is being dragged. This is exposed publ icly through the ShowColumnHeaderWhileDragging property.The only constructor in this class is a parameter-less one and is fairly straightforward There is, however, one line of code that I feel is worth mentioning.:
THIS.SETSTYLE (ControlStyles.doubleBuffer, true);
Painting in Windows is a two-step process. When an application makes a paint request, paint messages (WM_ERASEBKGND followed by WM_PAINT) are generated by the system. Those messages are sent to the application message queue where they are then examined by the application and routed to the appropriate control for handling. The default handling for the WM_ERASEBKGND message is to fill the area with the current window background color. The handling of WM_PAINT follows, which does all of the foreground painting. When you have a sequence that involves clearing the background and drawing in the foreground, you're creating an unpleasant effect known as flickering. fortunately, this can be alleviated by using double buffering.With double buffering, you have two different buffers to which you can write. One is the visible, on -Screen Buffer That Is Stored In The Video Ram, And The Other Is A Non-Visible, Off-Screen Buffer, Reperesented by An Internal GraphicsBuffer Object m RAM. When a drawing operation initiates, all of the graphics objects are rendered on the aforementioned GraphicsBuffer object. Once the system determines that the operation has completed, both buffers are quickly synchronized.
According to the .NET Framework documentation, in order to implement double buffering in your application, you need to set the AllPaintingInWmPaint, DoubleBuffer, and UserPaint ControlStyle bits to true. Here, I only needed to worry about the DoubleBuffer bit. The base DataGrid class Has Already Set The AllPaintingInWmPaint And Userpaint Bits To True.
Note the Other TWO
ControlStyle Bits Mentioned Above Are Defined As:
UserPaint: Setting this bit to true tells Windows that your application will take full accountability of all of the painting for that particular window (control) This means that you'll handle the WM_ERASEBKGND and WM_PAINT messages If this bit is set to false,.. the application will still hook the WM_PAINT message, but instead of performing any painting operations, it will send the message back to the system for handling. When this happens, the system attempts to render the window, but because it does not know anything about The window, IT Usually Doesn't Do a Very Good Job.allpaintingInwmpaint: As The Bit Name Indicates, When It Bit Is Set To True, All of The Painting Is Handled by Control's
WMPaint Method. THE WM_ERASEBKGND MESSAGE, EVEN THOUGH HOOKED, IGNORED AND THE CONTROL '
OneRaseBackground Method is never invoked.
Before deliving Into the rest of the class, there..............
INVALIDATION
When you invalidate a certain region of a control, it is added to the control's update region, which tells the system which area to repaint during the next painting operation. If the update region is not defined, the whole control is repainted.
Figure 2. Visual representation of an invalidated region before and after the painting operation has been triggered. On the left, the translucent grey square with the dotted border represents the defined invalidation region. The right square manifests the appearance after the painting operation has been performed .
When a control's invalidate method is invoked, as mentioned earlier, a WM_PAINT message is generated by the system and routed to the control. Upon receiving the message, the control raises the Paint event and if there is a registered handler listening for it, it is added to the back of the control's event handling queue.It is important to note that a raised Paint event does not always get handled immediately. There are a number of reasons for this, the most important being that the Paint event involves one of the more Expensive Operations in Drawing and is generally the last time. gets handled.
Grid Styles
A DataGridTableStyle defines how a DataGrid is drawn to the screen. Even though it contains properties that are similar to those of the DataGrid, they are mutually exclusive. A lot of people erroneously assume that changing a synonymous property, such as the DataGrid's RowHeadersVisible property, also changes the value of the DataGridTableStyle's' RowHeadersVisible property. As a result, time is unnecessarily spent debugging when things do not work as expected (do not worry I'm guilty of this too).
You Create a Collection of DiffERENT TABLES AND USE The InterchangeAbly with Sources.
Every DataGridTableStyle contains a GridColumnStylesCollection, which is a collection of DataGridColumnStyles objects that are automatically created when data is bound to the DataGrid control. Those objects are instances of DataGridBoolColumn, DataGridTextBoxColumn, or a third-party implemented column, which are all derived from DataGridColumnStyle. If you want a column tria Contains Labels, or EVEN images, the you will help you'llun to override the
OnDataSource Method, Which Is Invoked WHEN THE
DataGrid Control Is Bound to a Data Source. This allows you to use multiple style and correlate their maping names with the datagrid's
DataMember Property Value, Which is sethen the control is bound to the data source.
Column tracking
A vast majority of the column tracking functionality takes place in the MouseDown, MouseMove, and MouseUp event handlers. In the forthcoming paragraphs, I will focus on the three event handlers and provide explanations for the more important pieces of code. The helper methods that are Utilized by Those Handlers Are Not Discussed. However, IF You Take a Look At The Code, You'll See That I've Provided Summaries for Those Methods.
MouseDown
When the mouse is clicked above the grid, the first thing we need to do is determine where it was clicked. In order to initiate a drag, the cursor must have been clicked above a column header. If this condition proves to be true, some column information is gathered. We need to know the origin, width, and height of the column, as well as the mouse cursor's position relative to the column's origin. This information is used to establish the two different column regions that we are going to be Keeping TRACK OF WHILE The Column Is Being Dragged.private Void ColumnDragdataGrid_mousedown (Object Sender, MouseEventArgs E) {
DataGrid.hittestinfo hti = this.hittest (E.x, E.Y);
IF ((HTI.TYPE & DATAGRID.HITTESTTTYPE.COLUMNHEADER)! = 0 &&
THIS.M_DRAGGEDCOLUMN == NULL) {
INT XCoordinate = this.getleftmostColumnHeaderxcoordinate (HTI.COLUMN);
Int Ycoordinate = this.gettopmostColumnHeadrycoordinate (E.x, E.Y);
INT columnwidth = this.tablestyles [0] .gridcolumnStyles [hti.column] .width;
INT columnheight = this.getColumnheight (Ycoordinate);
Rectangle ColumnRegion = New Rectangle (XCoordinate, Ycoordinate, ColumnWidth, ColumnHeight);
Point StartingLocation = New Point (XCoordinate, Ycoordinate);
Point CursorLocation = New Point (E.X - XCoordinate, E.Y - YCOORDINATE);
Size columnsize = size.empty;
...
}
...
}
Figure 3. Diagram Showing Column Origin, Column Header Height As Calculate by The GetcolumnHeaderHeight Method, Column Height, Column Width, And Cursor Position
The rest of this event handler is fairly straightforward. A conditional evaluation is performed to see if the ShowColumnsWhileDragging or ShowColumnHeaderWhileDragging properties have been set to true. If so, the column sizes are calculated and the ScreenImage's GetScreenshot method is invoked. I pass the DataGrid control's handle (remember that a control is a child window), the starting coordinates and the column size and the method returns an image object containing the desired captured region. Everything is then stored in a DraggedDataGridColumn object.Private void ColumnDragDataGrid_MouseDown (object sender, MouseEventArgs e ) {
...
IF ((HTI.TYPE & DATAGRID.HITTESTTTYPE.COLUMNHEADER)! = 0 &&
THIS.M_DRAGGEDCOLUMN == NULL) {
...
IF (showcolumnwhiledragging || showcolumnheaderwhiledraging) {
IF (showcolumnwhiledraging) {
ColumnSize = New size (ColumnWidth, ColumnHeight);
} else {
Columnsize = New size (ColumnWidth, this.getColumnHeaderHeight (E.x, Ycoordinate));
}
Bitmap columnImage = (bitmap) ScreenImage.getscreenshot
This.Handle, StartingLocation, ColumnSize;
m_draggedcolumn = new DraggedDataGridColumn (HTI.COLUMN,
ColumnRegion, CursorLocation, ColumnImage
} else {
m_draggedcolumn = new DraggedDataGridColumn (HTI.COLUMN,
ColumnRegion, CursorLocation;
}
m_draggedcolumn.currentregion = columnRegion;
}
...
}
Mousemove
Every time the mouse cursor moves above the DataGrid, the MouseMove event is raised. In handling it, firstly I keep track of the column that the dragged column is currently hovering above so I can provide some visual feedback to the user. Secondly, I track down the new location of the column and deliver invalidation instructions.Let's take a closer look at the code. The first thing that I need to do is ensure that a column is being dragged, then I get the x-coordinate of the column by subtracting the mouse coordinates relative to the column's origin from the mouse coordinates relative to the control (Figure 4, marker # 1). This gives me the x-coordinate of the column. Because the y-coordinate never changes, I do not bother checking IT.
Private Void ColumnDragdataGrid_MouseMove (Object Sender, MouseEventArgs E) {
DataGrid.hittestinfo hti = this.hittest (E.x, E.Y);
IF (m_draggedcolumn! = null) {
INT x = E.X - m_draggedcolumn.cursorlocation.x;
...
}
}
Figure 4. MARKER # 1 Shows The Value That Is Stored In m_draggedColumn.cursorLocation.x. This value is subtracted from the capital.......................
I THEN CHECK TO SEE IF (The Column Headers Are Also Considered Cells). If it isn't, I Assume That the user ten to abort the drag operation.
Private Void ColumnDragdataGrid_MouseMove (Object Sender, MouseEventArgs E) {
...
IF (m_draggedcolumn! = null) {
IF (hti.column> = 0) {
...
} else {
INVALIDATECOLUMNAREA ();
RESETMEMBERSTODEFAULT ();
}
}
}
Next, I want to provide some kind of feedback to the user soful, where there. ..................
This is tracked through the m_mouseOverColumnIndex member field, which stores the index of the column whose boundaries contained the current location of the cursor after the last MouseMove event was handled. If this value is not the same as the column index that the hit test provides us with, then the user is hovering above a different column. If this is the case, the region indicated by the m_mouseOverColumnRect member field is invalidated and coordinates for the new region are recorded. The new region is then invalidated so Windows will know that new painting INSTRUCTIONS for this Area Await its attention.
Private Void ColumnDragdataGrid_MouseMove (Object Sender, MouseEventArgs E) {
...
IF (m_draggedcolumn! = null) {
...
IF (hti.column> = 0) {
IF (hti.column! = m_mouseovercolumnindex) {
// Note: moc = mouse over column
Int mocx = this.getleftmostColumnHeaderxcoordinate (HTI.COLUMN);
Int mocwidth =
This.Tables [0] .gridcolumnStyles [hti.column] .width;
// Indicate That We Want to Invalidate The Old Rectangle Area
IF (M_MouseOverColumnRect! = Rectangle.empty) {
This.INValidate (M_MouseOverColumnRect);
}
// if the mouse is hovering over the original color column, we do not want to @Want To
// Paint Anything, SO We Negate The Index.
IF (hti.column == m_draggedcolumn.index) {
m_mouseovercolumnindex = -1;
} else {
m_mouseovercolumnindex = hti.column;
}
M_MouseOverColumnRect = New Rectangle (MOCX,
m_draggedcolumn.initialregion.y, mocwidth,
m_draggedcolumn.initialregion.height);
// Invalidate this Area So it gets painted when onpaint is caled.
This.INValidate (M_MouseOverColumnRect);
}
...
Else {...}
}
}
Focus is then shifted towards tracking the location of the dragged column. I need to figure out if it's being dragged to the left or the right, so I can get the leftmost x-coordinate. Once this number has been obtained, the old and new Regions of the columns are invalidated and the data pertaining to the new location is stored in m_draggedcolumn.private void columnDragdataGrid_mousemove (Object sender, mouseeventargs e) {
...
IF (m_draggedcolumn! = null) {
...
IF (hti.column> = 0) {
...
INT OLDX = m_draggedcolumn.currentregion.x;
Point OldPoint = Point.empty;
// column is being Dragged to the Right
IF (Oldx OldPoint = New Point (Oldx - 5, M_DraggedColumn.initialRegion.y); // To the Left } else { Oldpoint = New Point (x - 5, m_draggedcolumn.initialregion.y); } Size SizeOfRectangletoinValidate = New size (Math.abs (X - OLDX) M_DraggedColumn.initialRegion.width (OldPoint.x * 2), M_DraggedColumn.initialRegion.Height); This.INValidate (New Rectangle (Oldpoint, SizeOfRectangletoinValidate); m_draggedcolumn.currentregion = New Rectangle (x, m_draggedColumn.initialRegion.y, m_draggedcolumn.initialregion.width, m_draggedcolumn.initialregion.Height); Else {...} } } Mouseup When the user releases the mouse button above a cell, a conditional evaluation is performed to ensure that the dragged column has been dropped above a column other than its originator. If the expression evaluates to true, as in the column index being different than the one WHERE IT Originated from, The Column Are Switch. OtherWise, The Grid Is Repainted. Private void columndragdataGrid_mouseup (Object sender, mouseeventargs e) { DataGrid.hittestinfo hti = this.hittest (E.X, E.Y); // Is Column Being Dropped Above Itself? If So, We don't want // to do anything IF (m_draggedcolumn! = null && hti.column! = m_draggedcolumn.index) { DataGridtableStyle DGTS = this.tablestyles [this.datamember]; DataGridColumnStyle [] columns = new DataGridColumnStyle [DGTS.GridColumnS.count]; // NOTE: CSI = ColumnStyleIndex For (int CSI = 0; csi IF (csi! = hti.column && csi! = m_draggedcolumn.index) { Columns [csi] = dgts.gridcolumnstyles [csi]; } else if (csi == hti.column) { Column [CSI] = DGTS.GridColumnStyles [m_draggedcolumn.index]; } else { Columns [csi] = dgts.gridColumnStyles [hti.column]; } } // Update TableStyle THIS.SUSPENDLAYOUT (); THIS.TABLESTYLES [this.datamember] .gridcolumnstyles.cle (); THIS.TABLESTYLES [this.datamember] .gridcolumnstyles.addrange (Column); THIS.ResumeLayout (); } else { INVALIDATECOLUMNAREA (); } RESETMEMBERSTODEFAULT (); } ......................... Overriding the datagrid's onpaint mix You might have noticed by now that none of the painting logic was performed in any of the mouse event handlers. It all boils down to a matter of preference. I've seen other developers entwine their paint logic with the rest of their logic, but I Find It Much Simpler and More Orderly To Keep All of The Paint Logic Withnin The onpaint method or the point event handler. The DataGrid's OnPaint method needed to be overridden in order to accommodate the additional paint operations. First in line is ensuring that the base OnPaint method is invoked so the underlying DataGrid is drawn. This gives me the canvas that I will be drawing on.Remember that . The First Shape That Gets Drawn Is The Rectangle That Used To Indicate Which Column Is Being Dragged (Figure 5, Marker # 1). Figure 5. The Different Drawing Steps By using the FillRectangle method of the Graphics object, we draw a rectangle over the column from which the drag originated. The region information is retrieved from the DraggedDataGridColumn object. A translucent brush is used so the underlying column is still visible. A black rectangle is THEN DRAWN Around The Border of the Aforementioned Rectangle To Give It A More Complete Touch. Protected Override Void OnPaint (Painteventargs E) { ... IF (m_draggedcolumn! = null) { Solidbrush BlackBrush = New Solidbrush (Color.Fromargb (255, 0, 0, 0)); Solidbrush Darkgreybrush = New Solidbrush (Color.Fromargb (150, 50, 50, 50); Pen Blackpen = New Pen (BlackBrush, 2F); G.FillRectangle (Darkgreybrush, M_DraggedColumn.initialRegion); G. DrawRectangle (Blackpen, Region); ... } } Colors in GDI are broken down into four 8-bit components, three of which represent the primary colors: red, green, and blue The Alpha component, also 8-bits, determines the transparency of the color, which influences how the colors blend. With the background. The color.fromargb method allows us to create a color with specificues.color.fromargb (150, 50, 50, 50) // Dark Grey with alpha translucency level set to 150 The column feedback that I mentioned earlier in the article is done in the form of a translucent light grey rectangle (Figure 5, marker # 2). First, I check the column index to ensure that it is not -1, and then fill a Rectangle over the column using The Rectangular Region Data Stored In m_mouseovercolumnRect. Protected Override Void OnPaint (Painteventargs E) { ... IF (m_draggedcolumn! = null) { // User feedback indeicating which column the Dragged Column Is over IF (this.m_mouseovercolumnindex! = -1) { Using (Solidbrush B = New Solidbrush (Color.Fromargb (100, 100, 100, 100))) { G.fillRectangle (B, M_MouseOverColumnRect); } } } } The next area of focus is the column that is being dragged. If the user has chosen to show the column or column header while the dragging operation is taking place, then the image is drawn. The captured image is stored in m_draggedColumn and is accessible through The ColumnImage Property. Protected Override Void OnPaint (Painteventargs E) { ... IF (m_draggedcolumn! = null) { ... // Draw Bitmap Image IF (showcolumnwhiledragging || showcolumnheaderwhiledraging) { g.drawImage (m_draggedcolumn.columnimage, m_draggedcolumn.currentregion.x, m_draggedcolumn.currentregion.y); } ... } } Finally, a translucent rectangle is filled to represent the dragging operation. This is done in a similar manner as the first shape. The column region information is read from m_draggedColumn. Another rectangle is then drawn to further enhance the previous rectangle (Figure 5, marker # 3) .protected override void onpaint (painteventargs e) { ... IF (m_draggedcolumn! = null) { ... g.fillRectangle (FilmFill, M_DraggedColumn.currentregion.x, M_DraggedColumn.currentregion.y, m_draggedcolumn.currentregion.width, m_draggedcolumn.currentregion.height); g.drawRectangle (Filmborder, New Rectangle) m_draggedcolumn.currentregion.x, m_draggedcolumn.currentregion.y Convert.TOINT16 (Filmborder.width), Width, Height); ... } } Conclusion In this article, I showed you how I was able to utilize some basic GDI functionality to achieve visual effects with the DataGrid control. By making calls across managed boundaries, I leveraged native GDI functionality to perform screen captures and use that in conjunction with the drawing Features in system.drawing to create an apppealing Drag-and-drop extraience.