mouse position in datagrid during MouseHover?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
How can you determine which cell the mouse is positioned over during the
mousehover event?
Thanks so much!
Mel
 
You can grab the mouse positon (Control.MousePosition) in the MouseHover
event handler, translate the coordinates into something appropriate to the
DataGrid (dataGrid1.PointToClient()), determine the portion of the DataGrid
over which the mouse is hovering (dataGrid1.HitTest()), and then examine the
HitTestInfo that is returned to determine if the mouse is over a cell and,
if so, which cell.

private void dataGrid1_MouseHover(object sender, System.EventArgs e)
{
DataGrid grid = (DataGrid)sender;
Point clientPos = grid.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo hitInfo = grid.HitTest(clientPos);
if (hitInfo.Type == DataGrid.HitTestType.Cell)
{
// this.dataGrid1[hitInfo.Row, hitInfo.Column]
}
}
 
Thanks so much!
Mel

Tim Wilson said:
You can grab the mouse positon (Control.MousePosition) in the MouseHover
event handler, translate the coordinates into something appropriate to the
DataGrid (dataGrid1.PointToClient()), determine the portion of the DataGrid
over which the mouse is hovering (dataGrid1.HitTest()), and then examine the
HitTestInfo that is returned to determine if the mouse is over a cell and,
if so, which cell.

private void dataGrid1_MouseHover(object sender, System.EventArgs e)
{
DataGrid grid = (DataGrid)sender;
Point clientPos = grid.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo hitInfo = grid.HitTest(clientPos);
if (hitInfo.Type == DataGrid.HitTestType.Cell)
{
// this.dataGrid1[hitInfo.Row, hitInfo.Column]
}
}

--
Tim Wilson
..NET Compact Framework MVP

melanieab said:
Hi,
How can you determine which cell the mouse is positioned over during the
mousehover event?
Thanks so much!
Mel
 
Back
Top