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;
//点击单元格获取单元格位置并显示控件A.png
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;
}
}
沒有留言:
張貼留言