【问题标题】:How to display tooltip for a cell in DataGridView based on the value present in the cell when mouse hovers over it当鼠标悬停在单元格上时,如何根据单元格中存在的值在 DataGridView 中显示单元格的工具提示
【发布时间】:2017-01-20 10:43:07
【问题描述】:

如上所述,考虑我的 DataGridView,当鼠标悬停在 NameID 字段中的单元格上时,根据单元格中存在的值 - 应该显示工具提示。 例如:如上图(图片),当鼠标悬停在 NameID 字段中的值 '3' 上时 - 'ABC' 显示为工具提示,同样对于 '1' 它应该显示 'DBC' 等等。

以下是我用 C#-Winforms 编写的代码,基于此链接中的文章:https://msdn.microsoft.com/en-us/library/2249cf0a(v=vs.110).aspx

但这似乎不起作用,甚至属性 ShowCellToolTips 都设为 True。

   void ToolTip1(object sender,DataGridViewCellFormattingEventArgs e)
   {
       if ((e.ColumnIndex == this.dataGridView1.Columns["NameID"].Index)
           && e.Value != null)
       {
           DataGridViewCell cell =
               this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
           if (e.Value.Equals("0"))
           {
               cell.ToolTipText = "Please update NameID as required, To know more click Help icon";
           }
           else if (e.Value.Equals("1"))
           {
               cell.ToolTipText = "DBC";
           }
           else if (e.Value.Equals("2"))
           {
               cell.ToolTipText = "XYZ";
           }
           else if (e.Value.Equals("3"))
           {
               cell.ToolTipText = "ABC";
           }

       }
   }

我怎样才能做到这一点?如何使这项工作?

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    你可以像这样使用CellMouseEnter事件:

    private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
                {
                    if ((e.ColumnIndex == this.dataGridView1.Columns["NameID"].Index))
                    {
                        //column name
                        DataGridViewCell cell =
                            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        //column id
                        DataGridViewCell cell1 =
                          this.dataGridView1.Rows[e.RowIndex].Cells["NameID"];
    
                        cell.ToolTipText = "DBC";
    
                        if (cell1.Equals("0"))
                        {
                            cell.ToolTipText = "Please update NameID as required, To know more click Help icon";
                        }
                        else if (cell1.Equals("1"))
                        {
                            cell.ToolTipText = "DBC";
                        }
                        else if (cell1.Equals("2"))
                        {
                            cell.ToolTipText = "XYZ";
                        }
                        else if (cell1.Equals("3"))
                        {
                            cell.ToolTipText = "ABC";
                        }
    
                    }
        }
    

    在这里你可以找到more

    【讨论】:

    • Exactly CellMouseEnter 事件在这种情况下完美运行。但是必须对代码进行一些修改才能使其正常工作。将在下面发布正确的代码
    • @JobAJ 你从来没有按照承诺发布正确的代码。现在这样做怎么样?
    • @Craig.Feed 现在还不是发布正确代码的好时机……或者 Will 应该发布正确的代码……
    猜你喜欢
    • 2019-02-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多