当鼠标位于 datagridview 行上方时,显示 datagridview 行中每个项目的工具提示
当您将鼠标悬停在特定行中的项目上时,如何为 datagridview
中的每个项目显示 datagridview
的工具提示?
我有一个带有列的表product
:
product name
product price
product description
product image ....
我要求有一个带有列的datagridview
,并且我从数据库中获取这些:
product name
product price
product image ....
现在我想显示这样的工具提示:如果我将鼠标悬停在产品图像上,将显示该产品的产品描述。我想对每一行都这样做。有人可以帮忙解决这个问题吗?
How can you show the tooltip for datagridview
for every item in datagridview
when you hover mouse over the item in that particular row?
I have table product
with columns:
product name
product price
product description
product image ....
I have a requirement that I have a datagridview
with columns and I am getting these from database:
product name
product price
product image ....
Now I want to show the tooltip like this: if I have mouse over the product image, the product description will be displayed for that product. I want to do this for every row. Would anyone please help on this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 DataGridViewCell.ToolTipText 属性 并使用 DataGridView 的
CellFormatting
事件来设置此属性值。您可以使用事件的DataGridViewCellFormattingEventArgs
ColumnIndex
属性来确定是否为要为其设置工具提示的列触发该事件,如果是,则使用事件的RowIndex
指定该工具提示的值。我链接的 MSDN 文章中的示例有一个很好的用法示例,但您的代码可能如下所示:
其中:
nameOrIndexOfYourImageColumn
= 图像列的列名称或索引值nameOrIndexOfYourDescriptionColumn
= 包含描述数据的列名称或索引值。注意:您需要某种方法来检索行的描述数据。执行此操作的常见方法是在 DataGridView 中为其设置一列,但由于您不想显示此列,因此请将其
Visible
属性设置为 false。然而还有其他选择。Take a look at the DataGridViewCell.ToolTipText property and use the DataGridView's
CellFormatting
event to set this property value. You can use the event'sDataGridViewCellFormattingEventArgs
ColumnIndex
property to determine if the event is firing for the column you want to set a tool tip for and if so use the event'sRowIndex
to specify that tool tip's value.The sample in the MSDN article I linked have a fine example of usage, but your code might look something like this:
Where:
nameOrIndexOfYourImageColumn
= the column name or index value of your image columnnameOrIndexOfYourDescriptionColumn
= the column name or index value with your description data.Note: that you'll need some way to retrieve a row's Description data. A common way to do this is to have a column for it in your DataGridView, but make since you don't want to display this column set its
Visible
property to false. There are other options however.我通过将要在每个单元格的工具提示中显示的文本存储在每个
DataGridViewCell
的Tag
属性中来完成此操作。然后,在
DataGridView.CellMouseEnter
事件中,您可以查看鼠标在哪个单元格中使用DataGridViewCellEventArgs.ColumnIndex
和DataGridViewCellEventArgs.RowIndex
值,并设置使用ToolTip.SetToolTip
将相应单元格中的文本作为工具提示文本。如果效果很好的话。
像这样的东西:
I have done this by storing the text to show in the tooltip for each cell in the
Tag
property of eachDataGridViewCell
.Then in the
DataGridView.CellMouseEnter
event you can see in which cell the mouse is using theDataGridViewCellEventArgs.ColumnIndex
andDataGridViewCellEventArgs.RowIndex
values and set the text from the corresponding cell as the tooltip text usingToolTip.SetToolTip
.If works pretty well.
Something like this:
填充
datagridview
时,只需将单元格的TooltipText
属性设置为您想要显示的文本即可。When filling the
datagridview
, just set theTooltipText
property of the cell to the text you want to display.