【问题标题】:Open a Form under a DataGridView Cell在 DataGridView 单元格下打开一个表单
【发布时间】:2021-06-07 17:56:42
【问题描述】:

我尝试在 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 单元格获得右上和左。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    如果您考虑使用表单,则必须使用屏幕坐标计算其位置:

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

    也看看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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多