3

How can you show the tooltip for datagridview when cell is selected, not from mouseover but from using the arrow keys?

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
Troy Mitchel
  • 1,790
  • 11
  • 50
  • 88

3 Answers3

7

As you've noticed, you won't be able to use the DataGridView's built in tooltip. In fact, you will need to disable it so set your DataGridView's ShowCellToolTips property to false (it's true by default).

You can use the DataGridView's CellEnter event with a regular Winform ToolTip control to display tool tips as the focus changes from cell to cell regardless of whether this was done with the mouse or the arrow keys.

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) {
    var cell = dataGridView1.CurrentCell;
    var cellDisplayRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
    toolTip1.Show(string.Format("this is cell {0},{1}", e.ColumnIndex, e.RowIndex), 
                  dataGridView1,
                  cellDisplayRect.X + cell.Size.Width / 2,
                  cellDisplayRect.Y + cell.Size.Height / 2,
                  2000);
    dataGridView1.ShowCellToolTips = false;
}

Note that I added an offset to the location of the ToolTip based on the cell's height and width. I did this so the ToolTip doesn't appear directy over the cell; you might want to tweak this setting.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • Important: You have to set the `ShowCellToolTips` property to `False`. I know it is stated in the answer, but I thought to give it more emphasis. – Nepaluz Apr 24 '17 at 10:36
0

Jay Riggs'answer is the one I used. Also, because I needed a much longer duration, I had to add this event in order to make the tooltip to dissapear.

private void dataGridView_MouseLeave(object sender, EventArgs e)
{
    toolTip1.Hide(this);
}
Alejandro del Río
  • 3,966
  • 3
  • 33
  • 31
-1
        dgv.CurrentCellChanged += new EventHandler(dgv_CurrentCellChanged);
    }

    void dgv_CurrentCellChanged(object sender, EventArgs e)
    {
        // Find cell and show tooltip.
    }
MrFox
  • 4,852
  • 7
  • 45
  • 81