Last night I discovery SourceGrid is not stopped on .NET 1.1, Davide Icardi version, but it’s maintained by dariusdamalakas (I don’t know if he’s the same guy, I think no), however is a great grid, but there was something missing really important for my project, lucky for me it’s quite simple to do, I need a columns drag and reorder or drag and swap reordering functionality.
First attempt [at the end failed] was to build a Controller for dragging headers, and it was working, but I cannot find a nice way to call column Swap and redraw header under mouse without changing more code that I was think it was a good code.
If I write too much code to do simple task, there’s a voice in me that tell me I’m wrong on what I’ve done.
So, trashed all tests, I prefer to keep original code “as is” and inherit original object:
* override OnMouseDown and save dragged column header
* override OnMouseMove to set destination column header by moving pointer
* override OnMouseUp to set final destination column header
* override OnPaint to redraw destination column header by moving pointer with a nice veil of transparent white
here a snapshot of result:
and here is the code:
using System;
using System.Drawing;
namespace SourceGrid_ColumnDragNSwap
{
public class SourceGrid_ColumnDragNSwap:SourceGrid.DataGrid
{
int draggingColumn = -1;
int targetColumn = -1;
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseDown(e);
draggingColumn = -1;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
if (this.MouseCellPosition.Row == 0) // header
draggingColumn = this.MouseCellPosition.Column;
}
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseMove(e);
if (draggingColumn != -1)
{
int newTargetColumn = this.PositionAtPoint(e.Location).Column;
if (targetColumn != newTargetColumn)
{
targetColumn = newTargetColumn;
// Invalidate to redraw all header cells
if (targetColumn != -1) this.InvalidateRange(new SourceGrid.Range(0, 0, 0, this.Columns.Count - 1));
}
}
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseUp(e);
if (draggingColumn != -1)
{
targetColumn = this.PositionAtPoint(e.Location).Column;
if (targetColumn != -1)
{
//Swap Columns
if (targetColumn != draggingColumn) this.Columns.Swap(targetColumn, draggingColumn);
targetColumn = -1;
}
draggingColumn = -1;
}
}
private Brush whiteVeilBrush = new SolidBrush(Color.FromArgb(150, Color.White));
private Rectangle cellVeilRectangle = new Rectangle();
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
//Paint over target header cell a white veil
if (draggingColumn != -1 && targetColumn != -1 && draggingColumn != targetColumn)
{
Rectangle currentCellRectangle = this.PositionToRectangle(new SourceGrid.Position(0, targetColumn));
cellVeilRectangle.X = currentCellRectangle.X;
cellVeilRectangle.Y = currentCellRectangle.Y;
cellVeilRectangle.Width = currentCellRectangle.Width - 2;
cellVeilRectangle.Height = currentCellRectangle.Height - 1;
e.Graphics.FillRectangle(whiteVeilBrush, cellVeilRectangle);
}
}
}
}
My source with test project is here
This work is based on SourceGrid:
Last source on https://bitbucket.org/dariusdamalakas/sourcegrid
Main site is http://sourcegrid.codeplex.com/
A great thing about SourceGrid, that’s all C# managed code, I want to try to see what happens on Mono…

Very useful snippet. Thank you.