c# - 当鼠标悬停在单元格上时,如何根据单元格中存在的值显示 DataGridView 中单元格的工具提示

标签 c# winforms datagridview

enter image description here

考虑我的 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";
           }

       }
   }

我怎样才能做到这一点?如何实现?

最佳答案

你可以像这样使用 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

关于c# - 当鼠标悬停在单元格上时,如何根据单元格中存在的值显示 DataGridView 中单元格的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761697/

相关文章:

c# - 雪花网连接器线程安全吗?

winforms - 在 DataGridView 中使用自定义格式化程序

c - 我怎样才能让 GetKeyState 理解大写和非大写字母

vb.net - VB : How to bind a DataTable to a DataGridView?

c# - 检查滚动条是否在数据 GridView 中可见

c# - 如何将 hierarchyid 列表转换为二叉树

c# 如何计算文本文件中的行数

c# - 将字体更改为 DataGridView 行在 WinForms C# 中不起作用

vb.net - Visual Basic,如何读取数据网格中的每一行?

c# - 在英特尔 phi 协处理器上运行 mono/c#