How can I make the datagrid have no currentcell

Platform: WinForms| Category: Datagrid

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.

public class MyDataGrid : DataGrid
{
	public const int WM_LBUTTONDOWN = 513; // 0x0201 
	public const int WM_LBUTTONUP = 514; // 0x0202 

	[System.Runtime.InteropServices.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( this.Handle, WM_LBUTTONDOWN, 0, 0);
		SendMessage( this.Handle, WM_LBUTTONUP, 0, 0);
	}
}

Here is some VB code.

Public Class MyDataGrid
  	Inherits DataGrid
  	
	Public WM_LBUTTONDOWN As Integer = 513 
   	Public WM_LBUTTONUP As Integer = 514

   	Shared  _
   	Function SendMessage(hWnd As IntPtr, msg As Int32, wParam As Int32, lParam As Int32) As Boolean
   
   	Public Sub SetNoCurrentCell()
     		 ’click on top left corner of the grid
     		 SendMessage(Me.Handle, WM_LBUTTONDOWN, 0, 0)
      		SendMessage(Me.Handle, WM_LBUTTONUP, 0, 0)
   	End Sub ’SetNoCurrentCell
End Class ’MyDataGrid

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.