Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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?

like image 941
Job AJ Avatar asked Oct 18 '22 18:10

Job AJ


1 Answers

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

like image 147
M. Wiśnicki Avatar answered Nov 01 '22 10:11

M. Wiśnicki