DataGridView工具提示文本未显示。

10

我在桌面应用程序中有一个数据绑定的 DataGridView,其中列的 ToolTipText 属性已设置,但当我悬停在网格视图(单元格或单元格标题)上时没有显示工具提示。

网格视图的 ShowCellToolTips 属性为 true,并且我已经使用断点验证了在鼠标悬停之前它没有被以编程方式更改。

我尝试创建一个 CellToolTipTextNeeded 事件处理程序来查看工具提示文本是什么,但是事件处理程序从未被调用。

我错过了什么吗?

谢谢, Rob

编辑:我们正在使用 framework 2.0。


我知道这是一个老问题,但是使用ToolTip组件进行黑客攻击真的是正确的答案吗?我们遇到了列/单元格工具提示未显示的同样问题。这似乎是DataGridView中应该得到修复的错误。 - Yoopergeek
@Yoopergeek 我同意,这是一个bug。我被告知在Framework 3.0中已经修复了,但由于我们的安装程序的限制,我们无法升级。 - Robert Gowland
1
我正在使用3.5版本...仍未修复。;) - Yoopergeek
12个回答

9

从您的问题中可以看出,您设置了列的工具提示文本。 只有在悬停在标题上时,列的工具提示文本才会显示。要在单元格上显示工具提示文本,您需要挂钩 CellToolTipTextNeeded 事件并在事件参数中设置 e.ToolTipText 的值。


谢谢回复,但正如问题所述: a)工具提示文本也没有显示在标题上 b)CellToolTipTextNeeded事件处理程序没有触发 - Robert Gowland
这段代码对我有效:private void dgvPlatypus_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { e.ToolTipText = "j. cutworm是个朋克"; } - B. Clay Shannon-B. Crow Raven
CellToolTipTextNeeded事件没有被触发。有什么提示吗? - Viku
在我的情况下,它也没有被触发,但我写了它并且它完美地工作了。this.dataGridView1.CellToolTipTextNeeded += dataGridView1_CellToolTipTextNeeded;private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { e.ToolTipText = "Trying trying trying"; } - Vbp

4
为了显示网格单元格的工具提示,您可以使用此事件处理程序“CellToolTipTextNeeded”。请参阅以下代码片段:
this.dataGridView1.ShowCellToolTips = true;
this.dataGridView1.CellToolTipTextNeeded += dataGridView1_CellToolTipTextNeeded;

void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)           
    {
        e.ToolTipText = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    }
}

4
尝试使用 Cell.ToolTipText 属性。您可能需要循环 DataGridView 的行并手动设置工具提示:
 For Each row As DataGridViewRow In Me.DataGridView.Rows
   Me.DataGridView("MyCol", row.Index).ToolTipText = "MyToolTipText"
 Next

对于有大量行的绑定DataGridView可能不太适合,但对于没有绑定且只有几百行的DataGridView,此方法在我的应用中运行成功。希望这可以帮助到您。


3
这是正确的答案。来自 MSDN:“仅在设置了 DataGridView 控件 DataSource 属性或其 VirtualMode 属性为 true 时,CellToolTipTextNeeded 事件才会发生。” - Kevin McCormick

4
当我在表单中添加了一个带有单个(空)列的DataGridView,为该列的ToolTipText属性添加了文本,并确保DataGridView的ShowCellToolTips属性设置为True时,当我将鼠标悬停在该列的标题上方时,我确实会弹出工具提示。这似乎与原始问题中所述的相矛盾,但在我的测试中,网格没有数据绑定。不确定这是否有所不同。不过,在一个具有数据绑定的DataGridView的项目中,我只是使用了一个ToolTip组件:
(1)向您的表单添加一个ToolTip组件。 (2)为您的DataGridView设置ToolTip on toolTip1 (或者用于您的ToolTip组件的等效名称)属性,以显示您想要显示的任何文本。 (3)将您的DataGridView的ShowCellToolTips属性设置为False。 (4)完成!按预期工作。

3
我们最终使用了一个ToolTip小部件和CellMouseEnterCellMouseLeave事件来适当地显示它。虽然不是最优解,但可以解决我们遇到的奇怪行为问题。

我能够通过将单元格标签设置为代码中我想要在特定单元格上设置警告的文本来绕过小部件,然后使用鼠标进入读取存储在单元格标签中的文本,并将datagridview单元格ToolTipText属性设置为我从标签设置的文本变量。当问题得到纠正并且鼠标进入为空字符串时,显然清除该标签。 - Krausladen

3

我曾遇到类似的问题,但是通过在DataGridView上将ShowCellToolTip设置为true,我成功地解决了它。一旦这样做,我就能发送以下代码并且一切都运作正常。

tableDocTypes.ShowCellToolTips = true;
tableDocTypes.Rows[i].Cells[columnFormCabinet.Index].ToolTipText = "Cabinet is not defined on the optical server.";

2

将datagridview的ShowCellToolTips属性设置为False


相反的方法对我起了作用-通过设置 dgv.ShowCellToolTips = true; ,然后列标题的工具提示开始显示。 - Paul

1

我目前在 Framework 3.5 上遇到了相同的问题。似乎需要设置 DataSource 属性才能触发 CelToolTipTextNeeded 事件。


@sindre 在我的情况下,数据源绝对已经设置,否则我的 GridView 中就不会有任何数据。 - Robert Gowland

0

我不确定这个提示是否是针对你特定问题的解决方案,但是你使用 VS2008 的 SP1 吗? 这个服务包解决了许多不同的问题,正如我所发现的。


@WunderWuzzi。不,我们没有安装SP1。我会与项目的技术负责人交谈,看看我们是否可以尝试安装它。然后我会更新这个问题。非常感谢。 - Robert Gowland

0

我在寻找每行设置工具提示的帮助时发现了这篇文章。

我只想确认在VS2008 SP1中处理CellToolTipText事件对我有效。

对于那些想知道如何将文本设置为基础数据行中的值的人,这可能会有用:

    private void myDGV_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
    {
        // This is used to set tooltiptext for individual cells in the grid.
        if (e.ColumnIndex == 2)  // I only want tooltips for the second column (0-based)
        {
            if (e.RowIndex >= 0)   // When grid is initialized rowindex == 0
            {
                // e.ToolTipText = "this is a test."; // static example.

                DataRowView drv = ((DataGridView)sender).Rows[e.RowIndex].DataBoundItem as DataRowView;
                MyTableRowClass theRow = drv.Row as MyTableRowClass;
                e.ToolTipText = theRow.Col1  + "\r\n" + theRow.Col2;
            }
        }
    }

这可能是一种更好的测试正确列的方法。如果您重新排列列的顺序,它仍将起作用。如果(myDGV.Columns [e.ColumnIndex] .Name ==“MyColumn”) - shindigo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接