Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DataGridView单元格显示时刷新其工具提示?_C#_.net_Winforms_Datagridview_Tooltip - Fatal编程技术网

C# 如何在DataGridView单元格显示时刷新其工具提示?

C# 如何在DataGridView单元格显示时刷新其工具提示?,c#,.net,winforms,datagridview,tooltip,C#,.net,Winforms,Datagridview,Tooltip,我正在尝试刷新DataGridViewCell的工具提示,而不使用光标离开并重新输入该单元格 我为单元格的ToolTipText属性指定了一个新值,但在显示工具提示时,更改列的ToolTipText属性不会对显示的工具提示产生任何影响 这是我的一个服务器应用程序的快照。人们可以加入,您可以看到他们的ping: 我希望能够看到ping是如何变化的。当显示单元格的工具提示时,更改单元格的工具提示文字不会自动更改工具提示文字。要更改它,可以处理DataGridView的CellToolTipChang

我正在尝试刷新DataGridViewCell的工具提示,而不使用光标离开并重新输入该单元格

我为单元格的ToolTipText属性指定了一个新值,但在显示工具提示时,更改列的ToolTipText属性不会对显示的工具提示产生任何影响

这是我的一个服务器应用程序的快照。人们可以加入,您可以看到他们的ping:

我希望能够看到ping是如何变化的。

当显示单元格的工具提示时,更改单元格的工具提示文字不会自动更改工具提示文字。要更改它,可以处理DataGridView的CellToolTipChanged事件,以检测单元格的ToolTipText中的更改。然后,您可以使用以下代码选中“使DataGridView在工具提示中显示新文本”:

private void dgv_CellToolTipTextChanged(object sender, DataGridViewCellEventArgs e)
{
    var grid = (DataGridView)sender;
    var toolTipControl = grid.GetType().GetField("toolTipControl",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance).GetValue(grid);
    var activated = (bool)toolTipControl.GetType()
        .GetProperty("Activated").GetValue(toolTipControl);
    if (activated)
    {
        var cell = grid[e.ColumnIndex, e.RowIndex];
        var ActivateToolTip = typeof(DataGridView).GetMethod("ActivateToolTip",
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance);
        ActivateToolTip.Invoke(grid,
            new object[] { true, cell.ToolTipText, e.ColumnIndex, e.RowIndex });
    }
}
范例

例如,我在计时器中更改了单元格的ToolTipText以获得以下结果:

private void timer1_Tick(object sender, EventArgs e)
{
    this.dataGridView1.Rows[0].Cells[0].ToolTipText = DateTime.Now.ToString();
}

上面是名为toolTipControl的字段,该集合在哪里?我刚刚处理了DataGridView的CellToolTipTextChanged事件。代码的其余部分是使用反射查找DataGridView的内部工具提示控件,并使其显示新的工具提示文本。因为当我尝试获取字段时,我得到的所有内容都是null。我想我遗漏了一些东西。只需像我一样创建一个简单的示例,并使用这些代码,截图取自代码的实际执行。解决了问题。为了将DoubleBuffer属性设置为true,我创建了一个自定义类。显然,这就是它找不到ToolTiControl字段的原因。无论如何,非常感谢你的帮助。