2011-07-20 57 views

回答

7

正如您已经注意到的,您将无法使用DataGridView的内置工具提示。实际上,您需要将其禁用,因此请将DataGridView的ShowCellToolTips属性设置为false(默认情况下为true)。

您可以将DataGridView的CellEnter事件与常规的Winform ToolTip控件一起使用,以显示工具提示,因为焦点在每个单元格之间变化,而不管这是使用鼠标还是箭头键完成的。

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

请注意,我根据单元格的高度和宽度向工具提示的位置添加了偏移量。我这样做了,所以ToolTip不会直接显示在单元格上;你可能想调整这个设置。

+0

嘿,这很好。非常感谢。 – TroyS

+0

重要提示:您必须将“ShowCellToolTips”属性设置为“False”。我知道答案中已经说明了这一点,但我认为应该更加重视。 – Nepaluz

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

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

Jay Riggs'answer是我用过的。另外,因为我需要更长的持续时间,所以我必须添加此事件才能使工具提示消失。

private void dataGridView_MouseLeave(object sender, EventArgs e) 
{ 
    toolTip1.Hide(this); 
} 
相关问题