当鼠标悬停在DataGridView单元格上时,如何根据单元格中的值显示提示信息。

6

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";
           }

       }
   }

我该如何实现这个?如何让它工作?

1个回答

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

                }
    }

在这里,你可以找到更多信息


在这种情况下,CellMouseEnter事件完美地工作。但是代码必须进行一些修改才能使其正常工作。将在下面发布正确的代码。 - Job AJ
@JobAJ,你之前承诺过发布正确的代码,但从未兑现。现在可以发布一下吗? - Craig.Feied
1
@Craig.Feied 现在不是发布正确代码的好时机...或者也许Will本来应该发布正确代码... - Heriberto Lugo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接