当鼠标位于数据网格中时,显示数据网格行中每个项目的工具提示

Eni*_*ate 29 .net c# datagridview tooltip winforms

当您将鼠标悬停在该特定行中的项目上时,如何显示datagridview每个项目的工具提示datagridview

我有表格product列:

product name 
product price 
product description
product image ....
Run Code Online (Sandbox Code Playgroud)

我有一个要求,我有一个datagridview列,我从数据库中获取这些:

product name 
product price 
product image ....
Run Code Online (Sandbox Code Playgroud)

现在我想显示这样的工具提示:如果我将鼠标悬停在产品图像上,将显示该产品的产品说明.我想为每一行做这件事.有人请帮忙吗?

Jay*_*ggs 42

查看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();
    }
}
Run Code Online (Sandbox Code Playgroud)

其中:
nameOrIndexOfYourImageColumn=图像列 nameOrIndexOfYourDescriptionColumn的列名称或索引值=包含描述数据的列名称或索引值.

注意:您需要某种方法来检索行的描述数据.执行此操作的常用方法是在DataGridView中为其创建一个列,但是因为您不希望显示此列,所以将其Visible属性设置为false.但是还有其他选择.


Pel*_*dao 5

我通过将文本存储在Tag每个DataGridViewCell.

然后,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));
    }
}
Run Code Online (Sandbox Code Playgroud)


Thu*_*rGr 5

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