Winforms-将新表单直接定位在Datagridview的父级选定行下 [英] Winforms - Position new form directly under Datagridview selected row of parent
问题描述
我有一个带有DataGridView的表单。我选择一行数据(总是单行)。单击按钮后,我想打开一个新表单并将其始终直接放置在所选行的下面。我应该怎么做?
I have a form with a DataGridView on it. I select a row (always a single row) of data. After I click a button I would like to open a new form and always position it directly under the selected row. How should I do this?
在我的按钮中,单击下面的按钮:
In my button click I do something like this:
private void myBtn_Click(object sender, EventArgs e)
{
if (myDGV.SelectedCells.Count > 0)
{
int i = myDGV.SelectedCells[0].RowIndex;
DataGridViewRow r = myDGV.Rows[i];
// READ SOME DATA FROM THE ROW
newForm form = new newForm();
//POPULATE THE NEW FORM WITH DATA
form.ShowDialog(this); //POSITION OF THIS FORM SHOULD BE DIRECTLY UNDER SELECTED ROW
}
}
推荐答案
您可以使用 DataGridView.GetRowDisplayRectangle 以获得数据网格视图客户端坐标中的行矩形,然后 Control.RectangleToScreen 将其转换为确定新表单位置所需的屏幕坐标,例如:
You can use DataGridView.GetRowDisplayRectangle to obtain the row rectangle in data grid view client coordinates, then Control.RectangleToScreen to convert them to a screen coordinates needed for determining the new form location, like this:
private void myBtn_Click(object sender, EventArgs e)
{
if (myDGV.SelectedCells.Count > 0)
{
int i = myDGV.SelectedCells[0].RowIndex;
DataGridViewRow r = myDGV.Rows[i];
// READ SOME DATA FROM THE ROW
newForm form = new newForm();
//POPULATE THE NEW FORM WITH DATA ...
// Position the form under the selected row
form.StartPosition = FormStartPosition.Manual;
var rect = myDGV.RectangleToScreen(myDGV.GetRowDisplayRectangle(i, false));
form.Location = new Point(rect.Left, rect.Bottom);
form.ShowDialog(this);
}
}
这篇关于Winforms-将新表单直接定位在Datagridview的父级选定行下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!