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

Job*_* AJ 5 c# datagridview winforms

在此处输入图片说明

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

       }
   }
Run Code Online (Sandbox Code Playgroud)

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

M. *_*cki 4

您可以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";
                    }

                }
    }
Run Code Online (Sandbox Code Playgroud)

在这里你可以找到更多