There appears to be no method to turn off a currentcell. When a cell is being edited, it is the TextBox embedded in the columnstyle that has the focus, and is displaying the highlighted text. You will notice in this situation, if you click the grid's title bar above the column headers, this TextEdit control loses focus, and the DataGrid appears to have no current cell.
We can simulate this click from code, and use it to expose a method in our DataGrid to SetNoCurrentCell. Below is some code to illustrate this idea.
using System.Runtime.InteropServices;
public class CustomDataGrid : DataGrid
{
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;
[ DllImport( "user32.dll" ) ]
static extern bool SendMessage(IntPtr hWnd, Int32 msg,
Int32 wParam, Int32 lParam);
public void SetNoCurrentCell()
{
//click on top left corner of the grid
SendMessage( Handle, WM_LBUTTONDOWN, 0, 0);
SendMessage( Handle, WM_LBUTTONUP, 0, 0);
}
}
[Visual Basic]
Public Class CustomDataGrid
Inherits DataGrid
Public Const WM_LBUTTONDOWN As Integer = &H201
Public Const WM_LBUTTONUP As Integer = &H202
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, _
ByVal wParam As Int32, ByVal lParam As Int32) As Boolean
End Function
Public Sub SetNoCurrentCell()
' click on top left corner of the grid
SendMessage(Handle, WM_LBUTTONDOWN, 0, 0)
SendMessage(Handle, WM_LBUTTONUP, 0, 0)
End Sub
End Class
Contributed from George Shepherd's Windows Forms FAQ