给定特定的行号和列索引,我如何计算 DataGridView 中的单元格位置(即:Location.Point)?
我需要单元格位置的原因是我可以在单元格内放置一个按钮以允许浏览文件夹(datagridview 显示文件夹路径)。
关于如何完成这种欢迎的替代建议。
最佳答案
您无法真正找到 DGV 单元格的点,因为单元格占据 DGV 中的矩形区域。但是,您可以使用 DataGridView.GetCellDisplayRectangle() method 找到此区域。 .它返回一个 Rectangle
,用于显示由 Cell 的列和行索引给出的 DGV Cell 的区域。如果您真的想要一个点,您可以轻松地使用 Rectangle
为 Rectangle
的四个角中的任何一个构造点。
// 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 并在那里托管您的 TextBox 和 Button。 Here's an example为 DateTimePicker 控件执行此操作:
关于c# - 确定 DataGridView 中的单元格位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5937140/