2017-11-17 155 views
0

我尝试在datagridview标题单元下方打开表单。我有这样的(和它不工作)datagridviewcell下的打开表格

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的单元格。

回答

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

如果您使用表单麻烦找youself,你可以考虑使用一个面板来代替:

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

也看看thisthis

+0

感谢,这工作。 – Hansvb