Troy Mitchel
Troy Mitchel

Reputation: 1810

Show tooltip when datagridview cell is selected

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

Upvotes: 3

Views: 12973

Answers (3)

Alejandro del Río
Alejandro del Río

Reputation: 4046

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

Upvotes: 0

Jay Riggs
Jay Riggs

Reputation: 53593

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.

Upvotes: 7

MrFox
MrFox

Reputation: 5116

        dgv.CurrentCellChanged += new EventHandler(dgv_CurrentCellChanged);
    }

    void dgv_CurrentCellChanged(object sender, EventArgs e)
    {
        // Find cell and show tooltip.
    }

Upvotes: -1

Related Questions