/// <summary>
        /// 根据鼠标位置返回dgv的行索引与列索引
        /// </summary>
        /// <param name="nX">当前X坐标</param>
        /// <param name="nY">当前Y坐标</param>
        /// <param name="dgv">DataGridView控件</param>
        /// <returns></returns>
        public static Point GetPointInf(int nX, int nY, DataGridView dgv)
        {
            int x = -1;
            int y = -1;
            for (int nR = 0; nR < dgv.Rows.Count; nR++)
            {
                Rectangle rectangle = dgv.GetRowDisplayRectangle(nR, false);
                if (dgv.RectangleToScreen(rectangle).Contains(nX, nY) == true)
                {
                    x = nR;
                }
            }
            for (int nC = 0; nC < dgv.Columns.Count; nC++)
            {
                Rectangle rec = dgv.GetColumnDisplayRectangle(nC, false);
                if (dgv.RectangleToScreen(rec).Contains(nX, nY) == true)
                {
                    y = nC;
                }
            }
            return new Point(x, y);
        }

  

posted on 2012-07-06 10:01  xalyf  阅读(1048)  评论(0编辑  收藏  举报