2015.1.4 判断鼠标点击DataGridView的第几行还是空白处

public int GetRowIndexAt(int mouseLocation_Y)

{

    if (dvaw.FirstDisplayedScrollingRowIndex < 0)

    {

      return -1;

     }

  if (dvaw.ColumnHeadersVisible == true && mouseLocation_Y <= dvaw.ColumnHeadersHeight)

      {

          return -1;

      }

 int index = dvaw.FirstDisplayedScrollingRowIndex;

 int displayedCount = dvaw.DisplayedRowCount(true);

  for (int k = 1; k <= displayedCount; )

            {

                if (dvaw.Rows[index].Visible == true)

                {

                    Rectangle rect = dvaw.GetRowDisplayRectangle(index, true);  // 取该区域的显示部分区域  

                    if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom)

                    {

                        return index;

                    }

                    k++;

                }

                index++;

            }

            return -1;

        }

posted on 2016-10-15 17:08  mol1995  阅读(572)  评论(0编辑  收藏  举报

导航