C# DataGridView ToolTipText 未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/389439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:37:00  来源:igfitidea点击:

DataGridView ToolTipText not showing

c#visual-studiovisual-studio-2008datagridviewdesktop-application

提问by Robert Gowland

I have data bound DataGridViewin a desktop app with columns that have their ToolTipTextproperty set, yet no tool tip is displayed when I hover over grid view (cells or cell headers).

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

The ShowCellToolTipsproperty of the grid view is true, and I have verified using break points that it is not changed programmatically before I mouse over.

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

I have tried creating a CellToolTipTextNeededevent handler to see what the tool tip text was, but the event handler is never called.

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

Is there anything I have missed?

有什么我错过了吗?

Thanks, Rob

谢谢,罗布

Edit:We're using framework 2.0.

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

采纳答案by Robert Gowland

We ended up using a ToolTip widget and the CellMouseEnter, CellMouseLeaveevents to show it appropriately. Not optimal, but it works around the odd behaviour we were experiencing.

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

回答by Marcus Tik

I don't know if this tip is a solution to your specific problem, but do you use SP1 of VS2008 ? This Service Pack resolves many different issues, as I have discovered.

我不知道这个技巧是否可以解决您的具体问题,但是您使用 VS2008 的 SP1 吗?正如我所发现的,此 Service Pack 解决了许多不同的问题。

回答by Marcus Tik

It appears from your question that you set the tooltip text of the columns. Columns tooltip text only appears when floating over the headers. To show tooltip text on the cells you have to hookup the CellToolTipTextNeededevent and set the value of e.ToolTipTextin the event args

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

回答by Simon

Try using Cell.ToolTipText property. You will probably need to loop the rows of the DataGridView and set the tooltips manually:

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

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

Might not be suitable for a bound DataGridView with lots of rows but works successfully for me with an unbound DataGridView with a couple of hundred rows. Hope this helps.

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

回答by sindre j

I'm currently experiencing the same behvoiur on Framework 3.5. Is seems the DataSource property needs to be set in order to get the CelToolTipTextNeeded event to fire.

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

回答by Nick Spreitzer

When I added a datagridview with a single (empty) column to a form, added text to the ToolTipText property for that column, and ensured that the ShowCellToolTips property for the datagridview is set to True, I do get a tooltip popup when I hover my mouse over that column's header. This seems to contradict what was stated in the original question, but in my test the grid was not data bound. Not sure if that makes a difference. However, on a project with a data bound datagridview, I just used a ToolTip component:

当我将带有单个(空)列的 datagridview 添加到表单,将文本添加到该列的 ToolTipText 属性,并确保 datagridview 的 ShowCellToolTips 属性设置为 True 时,当我将鼠标悬停在我的将鼠标悬停在该列的标题上。这似乎与原始问题中所述的内容相矛盾,但在我的测试中,网格不受数据约束。不确定这是否有区别。但是,在一个带有数据绑定 datagridview 的项目中,我只使用了一个 ToolTip 组件:

(1) Add a ToolTip component to your form.
(2) Set the ToolTip on toolTip1(or equivalent name for your ToolTip component) property for your datagridview to whatever text you want to display.
(3) Set your datagridview's ShowCellToolTips property to False.
(4) Viola! Works as expected.

(1) 在你的表单中添加一个 ToolTip 组件。
(2) 将ToolTip on toolTip1datagridview的(或 ToolTip 组件的等效名称)属性设置为要显示的任何文本。
(3) 将 datagridview 的 ShowCellToolTips 属性设置为 False。
(4) 中提琴!按预期工作。

回答by shindigo

I found this article looking for help on setting tooltips per row.

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

I just wanted to confirm that handling the CellToolTipText event works for me in VS2008 SP1.

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

For those of you who are wondering how the set the text to a value from the underlying datarow, this might be useful:

对于那些想知道如何将文本设置为底层数据行中的值的人来说,这可能很有用:

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

回答by JoBaxter

I had a simular problem but was able to correct it by setting the ShowCellToolTip to true on my DataGridView. Once I did that I was able to send the following code and everything worked fine.

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

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

回答by Alex Jolig

  1. set your DataGridView's ShowCellToolTipsproperty to false
  2. Put this code in your DataGridView's CellMouseEnterevent

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

回答by Muthukumar K

To show the tooltip of the grid cell, you can use this event handler "CellToolTipTextNeeded". Refer the below code Snippet,

要显示网格单元的工具提示,您可以使用此事件处理程序“ 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();
    }
}