DataGridViewCell.ToolTipText 属性


获取或设置与此单元格关联的 ToolTip 文本。Gets or sets the ToolTip text associated with this cell.

 property System::String ^ ToolTipText { System::String ^ get(); void set(System::String ^ value); };
public string ToolTipText { get; set; }
member this.ToolTipText : string with get, set
Public Property ToolTipText As 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 窗体 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 = 
        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 = 
        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 窗体 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.