Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to unselect the CurrentCell in a DataGrid

Status
Not open for further replies.

RexHeadlong

Programmer
Apr 10, 2002
35
US
I've got a derived DataGrid class and I need to be able to set the CurrentCell property to nothing.

I've used code I found on another website that sends a mousedown/mouseup message to the 0,0 pixel of the grid. This works in un-setting the CurrentCell, but the side-effect is that my mousedown and mouseup handlers get triggered, which is not a good thing in this case.

What I am really trying to do is to force the text box that is hosted by the grid column to lose its focus temporarily, so that listeners to the LostFocus event will be able to validate the contents of the cell.

So, I need a way to set the CurrentCell to nothing (without using mousedown/mouseup messages) or a way to unfocus the current cell's text box without putting any other cells into focus.

Anybody have any ideas?
Thanks in advance.
 
To set the focus on cell[i,j]:
Code:
m_DataGrid.CurrentCell = new DataGridCell(i,j);
If the grid has focus then the cell with focus is:
(m_DataGrid.CurrentCell.ColumnNumber, m_DataGrid.CurrentCell.RowNumber)

"a way to unfocus the current cell's text box without putting any other cells into focus"
This could be done by moving the focus on the host form of the DataGrid:

Example:
m_DataGrid is a grid in the 2nd page of a TabControl of a form.
Set focus on row 2 column 7:
Code:
dataGrid1.CurrentCell= new DataGridCell(2,7);
// Unfocus the grid 
this.tabControl2.TabPages[1].Focus();
Here is an example of using HitTestInfo , MouseDown, Click and CurrentCellChanged to change the cell with focus:

Code:
public class ResultsForm : System.Windows.Forms.Form
{
	private bool m_MouseEvent=false;
	private System.Windows.Forms.DataGrid.HitTestInfo m_HitTestInfo;
	private System.Windows.Forms.DataGrid m_DataGrid;

        private void InitializeComponent()
	{
		this.m_DataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_DataGrid_MouseDown);
		this.m_DataGrid.Click += new System.EventHandler(this.m_DataGrid_Click);
		this.m_DataGrid.CurrentCellChanged += new System.EventHandler(this.m_DataGrid_CurrentCellChanged);
	}
	private void m_DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
	{
		if (m_MouseEvent) 
		OnCellClick(m_DataGrid.CurrentCell.RowNumber,m_DataGrid.CurrentCell.ColumnNumber);
		m_MouseEvent=false;

	}
	private void m_DataGrid_Click(object sender, System.EventArgs e)
	{
		if (m_MouseEvent && m_HitTestInfo.Type==System.Windows.Forms.DataGrid.HitTestType.Cell)
			OnCellClick(m_HitTestInfo.Row,m_HitTestInfo.Column);
		else m_MouseEvent =false;
	}
	private void m_DataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
	{
		m_MouseEvent=true;
		m_HitTestInfo=m_DataGrid.HitTest(e.X,e.Y);
	}
	private void OnCellClick(int vRow, int vColumn)
	{
		
		m_MouseEvent=false;
		if (m_HitTestInfo.Row >= m_Rows)
		   return;   
		//Get Current DataGrid Cell contents
		//object vCell = m_DataGrid[vRow,vColumn];
		// Process here the click
		m_DataGrid[vRow,vColumn]=String.Empty;
	
	}


 }
obislavu
 
Thanks for the help.

I haven't tried your code, but what I found in the meantime was that a derived control can call its InvokeLostFocus method on a child control. So, I grab the text box associated with the DataGridTextBoxColumn, and then call myDataGrid.InvokeLostFocus(textBox).

This seems to work, except that the textBox can end up losing focus twice. It seems that in my event handler for when the textbox loses focus, any MessageBox that I display will cause the text box to lose focus a 2nd time. I just need to add a flag to prevent my lost focus handler from running a 2nd time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top