简体   繁体   English

单元格值更改时在DataGridView上显示工具提示

[英]Show ToolTip on DataGridView when cell value has changed

I want to show a ToolTip when the value of a cell in a DataGridView control is changed and the introduced value is invalid. DataGridView控件中单元格的值更改并且引入的值无效时,我想显示一个ToolTip

This can easily be done with the code below, but the problem is that after the ToolTip is shown, it is also associated with the control, so every time the DataGridView is hovered the ToolTip is displayed, and I want it to be shown only once after the data has been changed. 可以使用下面的代码轻松完成此操作,但是问题是在ToolTip之后,它也与控件相关联,因此,每次将DataGridView悬停时,都会显示该ToolTip ,并且我希望仅将其显示一次数据更改后。

I have noticed that this happens only with DataGridView and GroupBox . 我注意到,这仅在DataGridViewGroupBox With a TextBox or a Button the ToolTip is not associated with the control when shown over it. 使用TextBoxButtonToolTip在控件上方时不与控件关联。

Why does this happen? 为什么会这样?

    public partial class Form1 : Form
    {
        this.dataGridView1.ShowCellToolTips = false; // so we can show regular tooltip
    }
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView control = (DataGridView)sender;
        // check if control.Rows[e.RowIndex].Cells[e.ColumnIndex].Value is valid
        if (invalid)
        {
            toolTip.Show("Invalid data", dataGridView1, 5000);
        }
    }

There are many ways to deal with this. 有很多方法可以解决这个问题。 The simplest and most direct seems to be to Hide the ToolTip when you are leaving the DataGridView : 最简单,最直接的方法似乎是在离开DataGridViewHide ToolTip

private void dataGridView1_Leave(object sender, EventArgs e)
{
    toolTip1.Hide(this);
}

Of course it is up to you to decide upon the full design of what should happen while the error is still there..! 当然,完全由您决定在错误仍然存​​在的情况下应该如何处理的完整设计。

As for Textboxes : They are very special and there is usually no use in asking why they behave diffently.. 至于Textboxes :它们非常特殊,通常无用问为什么它们表现不同。

Another way would be to code the Popup event and use the Tag as a flag to make sure it is shown only once: 另一种方法是对Popup事件进行编码,并使用Tag作为标志以确保仅显示一次:

Before you show it in the CellValueChanged you set a flag: 在将其显示在CellValueChanged中之前,请设置一个标志:

 toolTip1.Tag = "true";

and in the Popup event you check for it: 然后在Popup事件中检查它:

private void toolTip1_Popup(object sender, PopupEventArgs e)
{
    if (toolTip1.Tag == null) return;
    bool show = toolTip1.Tag.ToString() == "true";
    if (toolTip1.Tag != "true") e.Cancel = true;
    toolTip1.Tag = "false";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户更改单元格值时捕获的 DataGridView 事件 - DataGridView Event to Catch When Cell Value Has Been Changed by User 选择 datagridview 单元格时显示工具提示 - Show tooltip when datagridview cell is selected 单元格值更改时更新 dataGridView - Update dataGridView when cell value changed DataGridView和BindingList:如何检查单元格值是否已更改? - DataGridView & BindingList : How to check if cell value has changed? 如何在鼠标悬停在单元格中时,根据单元格中存在的值显示DataGridView中单元格的工具提示 - How to display tooltip for a cell in DataGridView based on the value present in the cell when mouse hovers over it 确切在午夜时如何在Datagridview单元中显示完整的DateTime值? - How to show the complete DateTime value in a Datagridview cell when it is exactly midnight? 如何在DataGridView中更改单元格值时监视? - How to monitor whenever a cell value is changed in DataGridView? DataGridView获取预先更改的单元格值 - DataGridView get pre-changed cell value 当同一行中的另一个单元格发生更改时,如何更改DataGridView Cell的值? - How to change the value of DataGridView Cell when another cell in the same row changed? 确定何时更改DataGridView中的单元格,但仅在退出编辑模式后 - Determining when cell in DataGridView has changed, But only after exiting Edit Mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM