Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Winforms-将新表单直接放置在父级的Datagridview选定行下_C#_Winforms_Datagridview - Fatal编程技术网

C# Winforms-将新表单直接放置在父级的Datagridview选定行下

C# Winforms-将新表单直接放置在父级的Datagridview选定行下,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个带有DataGridView的表单。我选择一行(始终是单行)数据。单击某个按钮后,我希望打开一个新表单,并始终将其直接放在所选行的下方。我该怎么做 在我的按钮单击中,我执行以下操作: private void myBtn_Click(object sender, EventArgs e) { if (myDGV.SelectedCells.Count > 0) { int i = myDGV.SelectedCells

我有一个带有DataGridView的表单。我选择一行(始终是单行)数据。单击某个按钮后,我希望打开一个新表单,并始终将其直接放在所选行的下方。我该怎么做

在我的按钮单击中,我执行以下操作:

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
        }
    }
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);
    }
}
您可以使用获取数据网格视图客户端坐标中的行矩形,然后将其转换为确定新表单位置所需的屏幕坐标,如下所示:

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
        }
    }
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);
    }
}
您可以使用获取数据网格视图客户端坐标中的行矩形,然后将其转换为确定新表单位置所需的屏幕坐标,如下所示:

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

虽然Ivans的解决方案是100%有效和正确的,但就美学而言,它存在一些小的“抱怨”(他可以在优雅的解决方案中轻松满足这些抱怨)。如果希望对话框直接显示在行的下方,与gridview的左侧和行的底部齐平,则需要以不同的方式执行此操作—长距离

我重申这一点——Ivans解决方案非常合适,而且更干净。。下面的解决方案为您提供了更多关于表单放置等方面的自由。我在几分钟内就把它拼凑起来了,如果我错过了一些小东西,我很抱歉

您始终可以使用内置属性,如
Rectangle.Bottom
等。我这样做是为了,我想,展示如何处理事情的数学

private void myBtn_Click(object sender, EventArgs e)
{    
    if(dataGridView1.SelectedRows.Count > 0)
    {
        var rowIndex = myDGV.SelectedRows[0].Index;
        var row = myDGV.Rows[rowIndex];

        var formLocation = this.Location; //Form location
        var gridLocation = myDGV.Location; //grid location
        var rowLocation  = myDGV.GetRowDisplayRectangle(rowIndex, false).Location; //row location

        newForm form = new newForm();

        form.StartPosition     = FormStartPosition.Manual; //set to manual
        //form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        //form.BackColor       = Color.Blue;
        form.Location = GetPopupStartingLocation(
            new Point[] 
                { 
                    formLocation, 
                    gridLocation, 
                    rowLocation 
                }, 
            row.Height);

        form.Show(this);
    }
}

/*
A small helper method - didn't need to put this in a method
but I did it for clarity.
*/
 private Point GetPopupStartingLocation(Point[] locations, int rowHeight)
 {
    var yPadding = 5;
    var xValues  = locations.Sum(x => x.X);
    var yValues  = locations.Sum(x => x.Y) + ((rowHeight * 2) + yPadding);

    return new Point(xValues, yValues);
 }
现在,除了“更长的路线”之外,这与Ivan的基本相同,但是这让您可以使用helper函数对您的位置进行更多的控制。您可以添加/删除或调整下面的值,并找到您喜欢的内容。很好

希望这有帮助


+1向伊万索要干净的溶液。

虽然伊万的溶液100%有效且正确,但就美学而言,存在一些小的“牢骚”(他可以很容易地在优雅的溶液中满足这些牢骚)。如果希望对话框直接显示在行的下方,与gridview的左侧和行的底部齐平,则需要以不同的方式执行此操作—长距离

我重申这一点——Ivans解决方案非常合适,而且更干净。。下面的解决方案为您提供了更多关于表单放置等方面的自由。我在几分钟内就把它拼凑起来了,如果我错过了一些小东西,我很抱歉

您始终可以使用内置属性,如
Rectangle.Bottom
等。我这样做是为了,我想,展示如何处理事情的数学

private void myBtn_Click(object sender, EventArgs e)
{    
    if(dataGridView1.SelectedRows.Count > 0)
    {
        var rowIndex = myDGV.SelectedRows[0].Index;
        var row = myDGV.Rows[rowIndex];

        var formLocation = this.Location; //Form location
        var gridLocation = myDGV.Location; //grid location
        var rowLocation  = myDGV.GetRowDisplayRectangle(rowIndex, false).Location; //row location

        newForm form = new newForm();

        form.StartPosition     = FormStartPosition.Manual; //set to manual
        //form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        //form.BackColor       = Color.Blue;
        form.Location = GetPopupStartingLocation(
            new Point[] 
                { 
                    formLocation, 
                    gridLocation, 
                    rowLocation 
                }, 
            row.Height);

        form.Show(this);
    }
}

/*
A small helper method - didn't need to put this in a method
but I did it for clarity.
*/
 private Point GetPopupStartingLocation(Point[] locations, int rowHeight)
 {
    var yPadding = 5;
    var xValues  = locations.Sum(x => x.X);
    var yValues  = locations.Sum(x => x.Y) + ((rowHeight * 2) + yPadding);

    return new Point(xValues, yValues);
 }
现在,除了“更长的路线”之外,这与Ivan的基本相同,但是这让您可以使用helper函数对您的位置进行更多的控制。您可以添加/删除或调整下面的值,并找到您喜欢的内容。很好

希望这有帮助


+1向Ivan索取干净的溶液。

这需要一点数学知识:请检查您的
GridView。位置
,然后获取单击的行的索引(例如,
n
)。然后你的表单位置应该在
(gridview.location.x,gridview.location.Y+headerheight+(n+1)*行高)
中,这需要一点数学知识:请检查你的
gridview.location
,然后点击行的索引(比如,
n
)。那么您的表单位置应该在
(gridview.location.x,gridview.location.Y+headerheight+(n+1)*行高)