获取DataGridView中DataGridViewRow的坐标?

时间:2019-02-14 22:32:13

标签: c# winforms

我需要创建一个函数来获取Graphics.DrawLine()的数据行的左右坐标。

(int x, int y) GetCoordinate(DataGridViewRow row, char leftOrRight)
{
    if (leftOrRight == "L") {
        return ( ) // the left coordinates or current row if visible, 
                   // or the coordinates of the header line if it's not visible
    }
    else if (leftOrRight == "R") {
        return ( ) // the right coordinates or current row if visible, 
                   // or the coordinates of the header line if it's not visible
    }
}

用法:

var (x1, y1) = GetCoordinate(row1); // a row from data grid view 1
var (x2, y2) = GetCoordinate(row2); // a row from data grid view 2
graphics.DrawLine(pen, x1, y1, x2, y2);

如何从对象DataGridViewRow获取坐标?

1 个答案:

答案 0 :(得分:0)

您可以使用以下函数来获取(右,上)和(左,下)-这是当前行的边界框。

使用宽度和高度,您可以轻松推断出所有四个角的坐标。

    //X-cordiante of (right,top)
    double right = dataGridView1.CurrentRow.AccessibilityObject.Bounds.Right;
    //Y-cordiante of (right,top)
    double top = dataGridView1.CurrentRow.AccessibilityObject.Bounds.Top;
    //X-cordinate of (left,bottom)
    double left = dataGridView1.CurrentRow.AccessibilityObject.Bounds.Left;
    //Y-cordinate of (left,bottom)
    double bottom = dataGridView1.CurrentRow.AccessibilityObject.Bounds.Bottom;