当鼠标位于 datagridview 行上方时,显示 datagridview 行中每个项目的工具提示

发布于 2024-12-01 07:05:08 字数 436 浏览 1 评论 0原文

当您将鼠标悬停在特定行中的项目上时,如何为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-12-08 07:05:08

查看 DataGridViewCell.ToolTipText 属性 并使用 DataGridView 的 CellFormatting 事件来设置此属性值。您可以使用事件的 DataGridViewCellFormattingEventArgs ColumnIndex 属性来确定是否为要为其设置工具提示的列触发该事件,如果是,则使用事件的 RowIndex 指定该工具提示的值。

我链接的 MSDN 文章中的示例有一个很好的用法示例,但您的代码可能如下所示:

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 = 包含描述数据的列名称或索引值。

注意:您需要某种方法来检索行的描述数据。执行此操作的常见方法是在 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'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.

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();
    }
}

Where:
nameOrIndexOfYourImageColumn = the column name or index value of your image column
nameOrIndexOfYourDescriptionColumn = 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.

同尘 2024-12-08 07:05:08

我通过将要在每个单元格的工具提示中显示的文本存储在每个 DataGridViewCellTag 属性中来完成此操作。

然后,在 DataGridView.CellMouseEnter 事件中,您可以查看鼠标在哪个单元格中使用 DataGridViewCellEventArgs.ColumnIndexDataGridViewCellEventArgs.RowIndex 值,并设置使用 ToolTip.SetToolTip 将相应单元格中的文本作为工具提示文本。

如果效果很好的话。

像这样的东西:

private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= 0 & e.RowIndex >= 0) 
    {
        ToolTip1.SetToolTip(dgv, Convert.ToString(dgv.Item(e.ColumnIndex, e.RowIndex).Tag));
    }
}

I have done this by storing the text to show in the tooltip for each cell in the Tag property of each DataGridViewCell.

Then in the DataGridView.CellMouseEnter event you can see in which cell the mouse is using the DataGridViewCellEventArgs.ColumnIndex and DataGridViewCellEventArgs.RowIndex values and set the text from the corresponding cell as the tooltip text using ToolTip.SetToolTip.

If works pretty well.

Something like this:

private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= 0 & e.RowIndex >= 0) 
    {
        ToolTip1.SetToolTip(dgv, Convert.ToString(dgv.Item(e.ColumnIndex, e.RowIndex).Tag));
    }
}
时光瘦了 2024-12-08 07:05:08

填充datagridview时,只需将单元格的TooltipText属性设置为您想要显示的文本即可。

When filling the datagridview, just set the TooltipText property of the cell to the text you want to display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文