2011-07-20 100 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); 
} 
相關問題