02
2016
03

c# winform中如何取得 DataGridView 单元格的屏幕位置

 首先取得DataGridView的坐标位置:
int dgvX = dataGridView1.Location.X;
int dgvY = dataGridView1.Location.Y;
然后取得选中单元格的坐标在DataGridView中的坐标位置:
int cellX = dataGridView1.GetCellDisplayRectangle(ColumnIndex,RowIndex,false).X;
int cellY = dataGridView1.GetCellDisplayRectangle(ColumnIndex,RowIndex,false).Y;
最后可以得到每个单元格相对于form的坐标为:
int x = dgvX + cellX;
int y = dgvY + cellY;



//点击单元格获取单元格位置并显示控件

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex > 4)
            {
                int dgvX = dataGridView1.Location.X;
                int dgvY = dataGridView1.Location.Y;
                int cellX = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
                int cellY = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Y;
                int x = dgvX + cellX;
                int y = dgvY + cellY;
                dateTimePicker1.Location =  new Point(x,y);
                dateTimePicker1.Visible = true;
            }
        }


« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。