在DataGridView单元格下打开一个窗体 [英] Open a Form under a DataGridView Cell

查看:51
本文介绍了在DataGridView单元格下打开一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在DataGridView标头单元格下面打开一个窗体.我有这个(它不起作用)

I try to open a form just below a DataGridView header cell. I have this (and it is not working)

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单元格获得正确的顶部和左侧.

I don't see how to get the right top and left based on the DataGridView Cell.

推荐答案

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

Should you consider to use a Form, you have to calculate its Location using Screen coordinates:

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

如果您在使用表单时遇到麻烦,则可以考虑使用面板:

If you find youself in trouble using a Form, you might consider using a Panel instead:

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

还请参阅在Datagridview所选行的正下方直接放置新表单父母

这篇关于在DataGridView单元格下打开一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆