确定 DataGridView 中的单元格位置 [英] Determine cell location in DataGridView

查看:18
本文介绍了确定 DataGridView 中的单元格位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定特定的行号和列索引,如何计算 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 显示文件夹路径).

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 单元格的显示区域返回一个 Rectangle.如果你真的想要一个点,你可以很容易地使用 RectangleRectangle 的四个角中的任何一个构造点.

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}	 y:{1}", cellRectangle.Left, cellRectangle.Top);
Console.WriteLine("Bottom Right x:{0}	 y:{1}", cellRectangle.Right, cellRectangle.Bottom);

但我同意你的问题的评论者;最好创建一个自定义 DataGridViewColumn 并在那里托管您的 TextBox 和 Button.以下是为 DateTimePicker 控件执行此操作的示例:

But I agree with your question's commenters; it would be better to create a custom DataGridViewColumn and host your TextBox and Button there. Here's an example of doing this for the DateTimePicker control:

这篇关于确定 DataGridView 中的单元格位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆