显示当datagridview行中的每个项目的鼠标在其上方的工具提示 [英] Showing tool tip for every item in datagridview row when mouse is above it
问题描述
如果您将鼠标悬停在项目的上方,您如何显示 datagridview
中的 datagridview
中的每个项目的工具提示那个特定的行?
How can you show the tooltip for datagridview
for every item in datagridview
when you hover mouse over the item in that particular row?
我有表产品
与列:
product name
product price
product description
product image ....
我有一个要求,我有一个 datagridview
列,我从数据库中获取这些: p>
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?
推荐答案
看看 DataGridViewCell.ToolTipText属性,并使用DataGridView的 CellFormatting
事件设置此属性值。您可以使用事件的 DataGridViewCellFormattingEventArgs
ColumnIndex
属性来确定要为要设置工具的列触发事件提示,如果是,请使用事件的 RowIndex
来指定该工具提示的值。
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's DataGridViewCellFormattingEventArgs
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's RowIndex
to specify that tool tip's value.
MSDN文章中的示例我链接有一个很好的使用例子,但你的代码可能看起来像这样:
The sample in the MSDN article I linked have a fine example of usage, but your code might look something like this:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
if (e.ColumnIndex == dataGridView1.Columns[nameOrIndexOfYourImageColumn].Index) {
var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Set the Cell's ToolTipText. In this case we're retrieving the value stored in
// another cell in the same row (see my note below).
cell.ToolTipText = dataGridView1.Rows[e.RowIndex].Cells[nameOrIndexOfYourDescriptionColumn].Value.ToString();
}
}
其中:
nameOrIndexOfYourImageColumn
=您的图像列的列名称或索引值
nameOrIndexOfYourDescriptionColumn
=您的列名称或索引值说明数据。
Where:
nameOrIndexOfYourImageColumn
= the column name or index value of your image column
nameOrIndexOfYourDescriptionColumn
= the column name or index value with your description data.
注意:您需要一些方法来检索行的说明数据。一个常见的方法是在DataGridView中为其添加一列,但是由于您不想显示此列,将其 Visible
属性设置为false。还有其他选择。
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.
这篇关于显示当datagridview行中的每个项目的鼠标在其上方的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!