mouse position in datagrid during MouseHover?

G

Guest

Hi,
How can you determine which cell the mouse is positioned over during the
mousehover event?
Thanks so much!
Mel
 
T

Tim Wilson

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]
}
}
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top