Show tooltip when datagridview cell is selected
12,345
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.
Author by
Troy Mitchel
Updated on June 04, 2022Comments
-
Troy Mitchel almost 2 years
How can you show the tooltip for datagridview when cell is selected, not from mouseover but from using the arrow keys?
-
Nepaluz about 7 yearsImportant: You have to set the
ShowCellToolTips
property toFalse
. I know it is stated in the answer, but I thought to give it more emphasis.