DataGridViewCell.ToolTipText 屬性

定義

取得或設定與這個儲存格相關聯的工具提示文字。Gets or sets the ToolTip text associated with this cell.

public:
 property System::String ^ ToolTipText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string ToolTipText { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ToolTipText : string with get, set
Public Property ToolTipText As String

屬性值

String

與這個儲存格相關聯的工具提示文字。The ToolTip text associated with the cell. 預設值為 EmptyThe default is Empty.

屬性

範例

下列程式碼範例顯示如何 ToolTipText 在事件的事件處理常式中設定屬性 CellFormattingThe following code example shows how to set the ToolTipText property within an event handler for the CellFormatting event. 這個範例是 如何:將工具提示加入至 Windows Forms DataGridView 控制項中的個別儲存格中所提供的較大程式碼範例的一部分。This example is part of a larger code example provided in How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control.

// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(Object^ /*sender*/, 
    DataGridViewCellFormattingEventArgs^ e)
{
    if ( (e->ColumnIndex == this->dataGridView1->Columns["Rating"]->Index)
        && e->Value != nullptr )
    {
        DataGridViewCell^ cell = 
            this->dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];
        if (e->Value->Equals("*"))
        {                
            cell->ToolTipText = "very bad";
        }
        else if (e->Value->Equals("**"))
        {
            cell->ToolTipText = "bad";
        }
        else if (e->Value->Equals("***"))
        {
            cell->ToolTipText = "good";
        }
        else if (e->Value->Equals("****"))
        {
            cell->ToolTipText = "very good";
        }
    }
}
// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
        && e.Value != null )
    {
        DataGridViewCell cell = 
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (e.Value.Equals("*"))
        {                
            cell.ToolTipText = "very bad";
        }
        else if (e.Value.Equals("**"))
        {
            cell.ToolTipText = "bad";
        }
        else if (e.Value.Equals("***"))
        {
            cell.ToolTipText = "good";
        }
        else if (e.Value.Equals("****"))
        {
            cell.ToolTipText = "very good";
        }
    }
}
' Sets the ToolTip text for cells in the Rating column.
Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting

    If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
        AndAlso (e.Value IsNot Nothing) Then

        With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

            If e.Value.Equals("*") Then
                .ToolTipText = "very bad"
            ElseIf e.Value.Equals("**") Then
                .ToolTipText = "bad"
            ElseIf e.Value.Equals("***") Then
                .ToolTipText = "good"
            ElseIf e.Value.Equals("****") Then
                .ToolTipText = "very good"
            End If

        End With

    End If

End Sub

備註

當滑鼠指標位於資料格上方且屬性值不是時,這個屬性的值就會顯示為數據格工具提示 EmptyThe value of this property is displayed as the cell ToolTip when the mouse pointer is over the cell and the property value is not Empty. 如果這個屬性的值為 Empty ,則資料格會顯示包含儲存格值的工具提示(如果資料格顯示中的值已截斷),否則儲存格將不會顯示工具提示。If the value of this property is Empty, the cell will display a ToolTip containing the value of the cell if the value is truncated in the cell display; otherwise, the cell will not display a ToolTip. 您也可以將屬性設定為,以防止顯示工具提示 DataGridView.ShowCellToolTips falseYou can also prevent the display of a ToolTip by setting the DataGridView.ShowCellToolTips property to false.

DataGridView 控制項 DataSource 屬性已設定或其 VirtualMode 屬性為時 true ,取得屬性的值會 ToolTipText 引發控制項的 CellToolTipTextNeeded 事件,並傳回屬性的值, DataGridViewCellToolTipTextNeededEventArgs.ToolTipText 如事件處理常式中所指定。When the DataGridView control DataSource property is set or its VirtualMode property is true, getting the value of the ToolTipText property raises the CellToolTipTextNeeded event of the control and returns the value of the DataGridViewCellToolTipTextNeededEventArgs.ToolTipText property as specified in the event handler. 如果沒有事件的處理常式,取得屬性的值會傳回 ToolTipText 先前指定的值或其預設值 EmptyIf there are no handlers for the event, getting the value of the ToolTipText property returns the previously specified value or its default value of Empty.

在處理 CellToolTipTextNeeded 大量資料時,處理事件主要是很有用的,以避免在設定多個儲存格的儲存格值時發生效能上的負面影響 ToolTipTextHandling the CellToolTipTextNeeded event is primarily useful when working with large amounts of data to avoid performance penalties when setting the cell ToolTipText value for multiple cells. 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

變更這個屬性會 CellToolTipTextChanged 在擁有的上引發事件 DataGridView (如果有的話)。Changing this property raises the CellToolTipTextChanged event on the owning DataGridView, if one exists.

適用於

另請參閱