You can do this by deriving the DataGrid and overriding the OnMouseDown method. In the override, do a HitText and if the hit is on a column header that you do not want to sort, do not call the base class's method. Here is a code that sorts all columns except the second column
[C#]
using System.Drawing;
using System.Windows.Forms;
public class CustomDataGrid : DataGrid
{
protected override void OnMouseDown( MouseEventArgs e )
{
Point point = new Point( e.X, e.Y );
DataGrid.HitTestInfo hti = HitTest( point );
// don't sort column 1
if ( hti.Type == HitTestType.ColumnHeader && hti.Column == 1 )
return;
base.OnMouseDown(e);
}
}
[Visual Basic]
Public Class CustomDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
Dim point As New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = HitTest(point)
' don't sort column 1
If hti.Type = HitTestType.ColumnHeader AndAlso hti.Column = 1 Then
Return
End If
MyBase.OnMouseDown(e)
End Sub
End Class
Contributed from George Shepherd's Windows Forms FAQ