DataGridView ToolTipText未显示

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

网格视图的ShowCellToolTips属性为true ,我已经使用断点validation了在鼠标hover之前它没有以编程方式更改。

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

有什么我错过了吗?

谢谢,罗布

编辑:我们正在使用框架2.0。

从您的问题中可以看出,您设置了列的工具提示文本。 列工具提示文本仅在标题上方浮动时显示。 要在单元格上显示工具提示文本,您必须连接CellToolTipTextNeeded事件并在事件args中设置e.ToolTipText的值

尝试使用Cell.ToolTipText属性。 您可能需要循环DataGridView的行并手动设置工具提示:

  For Each row As DataGridViewRow In Me.DataGridView.Rows Me.DataGridView("MyCol", row.Index).ToolTipText = "MyToolTipText" Next 

可能不适合具有大量行的绑定DataGridView,但对于我来说,使用带有几百行的未绑定DataGridView可以成功运行。 希望这可以帮助。

当我将带有单个(空)列的datagridview添加到表单时,将文本添加到该列的ToolTipText属性中,并确保将datagridview的ShowCellToolTips属性设置为True,当我将鼠标hover在my时,我会获得工具提示弹出窗口将鼠标hover在该列的标题上。 这似乎与原始问题中所述的内容相矛盾,但在我的测试中,网格不是数据绑定的。 不确定这是否有所作为。 但是,在具有数据绑定datagridview的项目中,我只使用了ToolTip组件:

(1)向表单添加ToolTip组件。
(2)将ToolTip on toolTip1 (或ToolTip组件的等效名称)属性ToolTip on toolTip1ToolTip on toolTip1设置为要显示的任何文本。
(3)将datagridview的ShowCellToolTips属性设置为False。
(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(); } } 

我们最终使用ToolTip小部件和CellMouseEnterCellMouseLeave事件来适当地显示它。 不是最佳的,但它可以解决我们遇到的奇怪行为。

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

我有一个类似的问题,但能够通过在我的DataGridView上将ShowCellToolTip设置为true来纠正它。 一旦我这样做,我能够发送以下代码,一切正常。

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

将datagridview ShowCellToolTips属性设置为False

我不知道这个提示是否是您特定问题的解决方案,但是您使用VS2008的SP1吗? 正如我所发现的,此Service Pack解决了许多不同的问题。

我发现这篇文章寻求有关每行设置工具提示的帮助。

我只是想确认在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; } } } 
  1. 将DataGridView的ShowCellToolTips属性设置为false
  2. 将此代码放在DataGridView的CellMouseEnter事件中

     private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { if (!(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewImageCell))) return; System.Windows.Forms.ToolTip tlp = new System.Windows.Forms.ToolTip(); tlp.SetToolTip(dgv, "Your ToolTipText"); }