根据鼠标坐标值,获取控件DataGridView的行、列索引值

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 14:51   11   0
 /// <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);
        }

  

转载于:https://www.cnblogs.com/xalyf/archive/2012/07/06/2578936.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP