DataGridViewCell.ToolTipText 属性

定义

获取或设置与此单元格关联的 ToolTip 文本。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; }
member this.ToolTipText : string with get, set
Public Property ToolTipText As String

属性值

与该单元格关联的工具提示文本。The ToolTip text associated with the cell. 默认值为 EmptyThe default is Empty.

属性

示例

下面的代码示例演示如何在事件处理程序中设置 CellFormatting 事件的 ToolTipText 属性。The 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 = 
            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

注解

当鼠标指针位于单元格上并且属性值不 Empty时,此属性的值将显示为单元格工具提示。The 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 属性设置为 false来阻止显示工具提示。You can also prevent the display of a ToolTip by setting the DataGridView.ShowCellToolTips property to false.

当设置 DataGridView 控件 DataSource 属性或 trueVirtualMode 属性时,获取 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 事件主要是非常有用的,目的是为多个单元格设置单元 ToolTipText 值时避免性能下降。Handling 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.

更改此属性将引发所属 DataGridView上的 CellToolTipTextChanged 事件(如果存在)。Changing this property raises the CellToolTipTextChanged event on the owning DataGridView, if one exists.

适用于

另请参阅