【问题标题】:Determine cell location in DataGridView确定 DataGridView 中的单元格位置
【发布时间】:2011-08-21 16:23:42
【问题描述】:

给定特定的行号和列索引,我如何计算 DataGridView 内的单元格位置(即:Location.Point)?

我需要单元格位置的原因是我可以在单元格内放置一个按钮以允许浏览文件夹(datagridview 显示文件夹路径)。

关于如何实现这一欢迎的替代建议。

【问题讨论】:

  • 你的想法很脆弱。有更好的方法可以将按钮添加到 DataGridView 中的单元格。单元格可以包含您喜欢的任何类型的控件,包括自定义控件。我建议先检查一下。
  • 为什么不用DataGridViewButtonColumn?它是 .NET Framework 的一部分。
  • 因为我想要实现的是一个旁边有一个按钮的文本框(我确信有更好的方法,但不能完全解决 - 可能继承 DataGridViewColumn)。
  • 我会使用两列...我不明白为什么它们必须在同一个单元格中。

标签: c# .net winforms data-binding datagridview


【解决方案1】:

您无法真正找到 DGV 单元的点,因为单元在 DGV 中占据一个矩形区域。但是,您可以使用DataGridView.GetCellDisplayRectangle() method 找到该区域。它返回一个Rectangle,用于由单元格的列和行索引给出的 DGV 单元格的显示区域。如果您真的想要一个点,您可以轻松地使用RectangleRectangle 的四个角中的任何一个构造点。

// 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 控件执行此操作:

【讨论】:

  • 我最终使用了您关于创建列的正确方法的建议。它运作良好。谢谢!
  • 你提供的这个 MSDN 正是我要找的。​​span>
猜你喜欢
  • 1970-01-01
  • 2018-01-01
  • 2013-04-15
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多