C# 在DataGridView单元格下打开窗体

C# 在DataGridView单元格下打开窗体,c#,winforms,datagridview,C#,Winforms,Datagridview,我尝试在DataGridView标题单元格下方打开一个表单。我有这个(它不工作): < >我不知道如何根据 DATGRIDVIEW < P>获取正确的左上角和右下角。如果你考虑使用一个表单,你必须使用屏幕坐标计算它的位置: Form form = new Form(); form.StartPosition = FormStartPosition.Manual; form.FormBorderStyle = FormBorderStyle.FixedSingle; form.Size = new

我尝试在
DataGridView
标题单元格下方打开一个表单。我有这个(它不工作):


< >我不知道如何根据<代码> DATGRIDVIEW < P>获取正确的左上角和右下角。如果你考虑使用一个表单,你必须使用屏幕坐标计算它的位置:

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);

如果你发现自己在使用一个窗体时遇到麻烦,你可以考虑用一个面板代替:

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();
还可以看看

如果你考虑使用一个窗体,你必须使用屏幕坐标计算它的位置:

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);

如果你发现自己在使用一个窗体时遇到麻烦,你可以考虑用一个面板代替:

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();
还可以看看