简体   繁体   中英

How to display tooltip for a cell in DataGridView based on the value present in the cell when mouse hovers over it

在此输入图像描述

Consider my DataGridView as above, when mouse hovers over a cell in NameID field, based on the value present in cell-should display the tooltip. For example: As shown above(Image), when mouse hovers over the value '3' in NameID field - 'ABC' is shown as tooltip, similarly for '1' it should show 'DBC' and so on.

Below is code I have written in C#-Winforms, based on article found in this link: https://msdn.microsoft.com/en-us/library/2249cf0a(v=vs.110).aspx

But This doesn't seem work, Even the property ShowCellToolTips is made 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";
           }

       }
   }

How can I achieve this? how to make this work?

You can just use CellMouseEnter event like this:

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

                }
    }

Here you find more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM