3
Answers

Getting cursor position from grid view

Kok

Kok

16y
13.7k
1

Hi! I have a grid view where I would like to get the cursor position from it when user fired event key up. I try to use code as below but it failed because the X and Y position from cursor.position is always out of range. Does anyone know how to solve it? Thanks.

private DataGridViewCell clickedCell;
Point cursorPos = dg_item.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
                DataGridView.HitTestInfo hit = dg_item.HitTest(cursorPos.X, cursorPos.Y);
                if (hit.Type == DataGridViewHitTestType.Cell)
                {
                    MessageBox.Show("true");
                    clickedCell =
                        dg_item.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
                }

Answers (3)