There are a couple of other ways to get tooltips on DataGridView.
Each cell has its own tooltip in the cells DataGridViewCell.ToolTipText property.
E.g. dataGridView1[2, 3].ToolTipText = "Cell Tip Text";
This is fine if the text will never change. (But might have a performace hit if setting values for a large number of cells).
Using the CellToolTipTextNeeded event is also possible. To use this you should set a DataSource for the grid or set the Virtual property true. The code sample below shows how to use this.
private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
e.ToolTipText = string.Format("This cell is in row {0}, col {1}", e.RowIndex, e.ColumnIndex);
}
However, I personally do not like the way the DataGridView places the tooltip right over the cell. So I use a ToolTip object instead. Your code is fine but needs a little adjustment.
First, put the ToolTip at form scope. This is to ensure the ToolTip object is not disposed when the CellMouseEnter event exits.
Add a panel to your form and drag/drop your datagrid in to it. Set Dock on the datagrid to Fill and position the panel how you like. (This is because the X and Y on ToolTip.Show are relative to the top left corner of the "window" object passed to it and we need to use an object otherthan the DataGridView because that hides our tooltips.)
You also need to get the X and Y values for the cell so you can place the tip properly.
ToolTip tooltip1 = new ToolTip(); // <-- ToolTip at form scope
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex != -1) && (e.ColumnIndex == 1))
{
//MessageBox.Show("TEST");
// get x and y position of cell
Rectangle rec = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Point p = rec.Location;
// add offset to move tip up
p.Offset(0, -(20));
// changed dataGridView1 to panel1, use offset cell position, and timeout
tooltip1.Show("hello", panel1, p, 2500); // show tool tip
}
}
That's quite a lot of messing about and you might prefer the simpler first option.
Let me know how you get on.