在DataGridView中确定细胞的位置细胞、位置、DataGridView

2023-09-03 11:02:45 作者:突然好想我男票

给出了具体的行数和列索引我如何计算的单元格位置?:一个DataGridView里面(IE Location.Point)

Given a specific row number and column index how can I calculate the cell location (IE: Location.Point) inside a DataGridView?

我需要的单元格的位置的原因是这样我就可以在单元格内放置一个按钮,允许文件夹浏览(在DataGridView显示folderpaths)。

The reason I need the location of the cell is so I can position a button inside the cell to allow for folder browsing (the datagridview shows folderpaths).

替代有关如何做到这一点的欢迎建议。

Alternative suggestions about how to accomplish this welcome.

推荐答案

您真的不能找到一个点,一个DGV细胞,因为细胞占据了矩形区域在DGV。但是,您可以通过找到这方面的DataGridView.GetCellDisplayRectangle()法。它返回一个矩形由单元格的列和行索引给出DGV单元的显示区域。如果你真的想要一个点,你可以很容易地使用矩形来构建点对任何矩形的四角

You can't really find a point for a DGV cell because cells occupy a rectangular area in a DGV. However, you can find this area by using the DataGridView.GetCellDisplayRectangle() method. It returns a Rectangle for the display area of a DGV Cell given by the Cell's column and row indices. If you really want a point you can easily use the Rectangle to construct Points for any of the Rectangle's four corners.

// Get Rectangle for second column in second row.
var cellRectangle = dataGridView1.GetCellDisplayRectangle(1, 1, true);
// Can create Points using the Rectangle if you want.
Console.WriteLine("Top Left     x:{0}\t y:{1}", cellRectangle.Left, cellRectangle.Top);
Console.WriteLine("Bottom Right x:{0}\t y:{1}", cellRectangle.Right, cellRectangle.Bottom);

不过,我同意你的问题的评议;这将是最好创建一个自定义的DataGridViewColumn和托管您的文本框和按钮那里。 这里是做这行的DateTimePicker控件的一个例子: