我尝试在datagridview标题单元格下面打开一个表单。我有这个(它没有用)
private void button1_Click(object sender, EventArgs e)
{
Form aForm = new Form();
aForm.Text = @"Test";
aForm.Top = this.Top + dataGridView1.Top - dataGridView1.GetCellDisplayRectangle(0, 0, false).Height;
aForm.Left = this.Left + dataGridView1.GetCellDisplayRectangle(0, 0, false).Left;
aForm.Width = 25;
aForm.Height = 100;
aForm.ShowDialog();
}
我不知道如何根据datagridview单元格获得正确的左上角和右上角。
答案 0 :(得分:2)
如果您考虑使用表单,则必须使用屏幕坐标计算其位置:
Form form = new Form();
form.StartPosition = FormStartPosition.Manual;
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.Size = new Size(dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Width, 100);
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
form.Location = new Point(c.X, c.Y);
form.BringToFront();
form.Show(this);
如果您发现使用表单时遇到问题,可以考虑改用Panel:
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
Point r = this.PointToClient(c);
panel1.Location = new Point(r.X, r.Y);
panel1.BringToFront();
另请参阅How do I to get the current cell position x and y in a DataGridView?
和Position new form directly under Datagridview selected row of parent