DataGridViewCellToolTipTextNeededEventArgs DataGridViewCellToolTipTextNeededEventArgs DataGridViewCellToolTipTextNeededEventArgs DataGridViewCellToolTipTextNeededEventArgs Class

定义

CellToolTipTextNeeded 事件提供数据。Provides data for the CellToolTipTextNeeded event.

public ref class DataGridViewCellToolTipTextNeededEventArgs : System::Windows::Forms::DataGridViewCellEventArgs
public class DataGridViewCellToolTipTextNeededEventArgs : System.Windows.Forms.DataGridViewCellEventArgs
type DataGridViewCellToolTipTextNeededEventArgs = class
    inherit DataGridViewCellEventArgs
Public Class DataGridViewCellToolTipTextNeededEventArgs
Inherits DataGridViewCellEventArgs
继承
DataGridViewCellToolTipTextNeededEventArgsDataGridViewCellToolTipTextNeededEventArgsDataGridViewCellToolTipTextNeededEventArgsDataGridViewCellToolTipTextNeededEventArgs

示例

以下代码示例填充ToolTipText隐藏的单元中的信息DataGridViewThe following code example fills the ToolTipText with information from hidden cells in the DataGridView.

void dataGridView1_CellToolTipTextNeeded(object sender,
    DataGridViewCellToolTipTextNeededEventArgs e)
{
    string newLine = Environment.NewLine;
    if (e.RowIndex > -1)
    {
        DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

        // Add the employee's ID to the ToolTipText.
        e.ToolTipText = String.Format("EmployeeID {0}:{1}",
            dataGridViewRow1.Cells["EmployeeID"].Value, newLine);

        // Add the employee's name to the ToolTipText.
        e.ToolTipText += String.Format("{0} {1} {2}{3}",
            dataGridViewRow1.Cells["TitleOfCourtesy"].Value.ToString(),
            dataGridViewRow1.Cells["FirstName"].Value.ToString(),
            dataGridViewRow1.Cells["LastName"].Value.ToString(),
            newLine);

        // Add the employee's title to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}",
            dataGridViewRow1.Cells["Title"].Value.ToString(),
            newLine, newLine);

        // Add the employee's contact information to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}, ",
            dataGridViewRow1.Cells["Address"].Value.ToString(), newLine,
            dataGridViewRow1.Cells["City"].Value.ToString());
        if (!String.IsNullOrEmpty(
            dataGridViewRow1.Cells["Region"].Value.ToString()))
        {
            e.ToolTipText += String.Format("{0}, ",
                dataGridViewRow1.Cells["Region"].Value.ToString());
        }
        e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}",
            dataGridViewRow1.Cells["Country"].Value.ToString(),
            dataGridViewRow1.Cells["PostalCode"].Value.ToString(),
            newLine, dataGridViewRow1.Cells["HomePhone"].Value.ToString(),
            dataGridViewRow1.Cells["Extension"].Value.ToString(),
            newLine, newLine);

        // Add employee information to the ToolTipText.
        DateTime HireDate =
            (DateTime)dataGridViewRow1.Cells["HireDate"].Value;
        e.ToolTipText +=
            String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}",
            HireDate.Month.ToString(), HireDate.Day.ToString(),
            HireDate.Year.ToString(), newLine,
            dataGridViewRow1.Cells["Manager"].Value.ToString());
    }
}
Public Sub dataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _
    ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _
    Handles dataGridView1.CellToolTipTextNeeded

    Dim newLine As String = Environment.NewLine
    If e.RowIndex > -1 Then
        Dim dataGridViewRow1 As DataGridViewRow = _
        dataGridView1.Rows(e.RowIndex)

        ' Add the employee's ID to the ToolTipText.
        e.ToolTipText = String.Format("EmployeeID {0}: {1}", _
            dataGridViewRow1.Cells("EmployeeID").Value.ToString(), _
            newLine)

        ' Add the employee's name to the ToolTipText.
        e.ToolTipText += String.Format("{0} {1} {2} {3}", _
            dataGridViewRow1.Cells("TitleOfCourtesy").Value.ToString(), _
            dataGridViewRow1.Cells("FirstName").Value.ToString(), _
            dataGridViewRow1.Cells("LastName").Value.ToString(), _
            newLine)

        ' Add the employee's title to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}", _
            dataGridViewRow1.Cells("Title").Value.ToString(), _
            newLine, newLine)

        ' Add the employee's contact information to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}, ", _
            dataGridViewRow1.Cells("Address").Value.ToString(), newLine, _
            dataGridViewRow1.Cells("City").Value.ToString())
        If Not String.IsNullOrEmpty( _
            dataGridViewRow1.Cells("Region").Value.ToString())

            e.ToolTipText += String.Format("{0}, ", _
               dataGridViewRow1.Cells("Region").Value.ToString())
        End If
        e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}", _
            dataGridViewRow1.Cells("Country").Value.ToString(), _
            dataGridViewRow1.Cells("PostalCode").Value.ToString(), _
            newLine, _
            dataGridViewRow1.Cells("HomePhone").Value.ToString(), _
            dataGridViewRow1.Cells("Extension").Value.ToString(), _
            newLine, newLine)

        ' Add employee information to the ToolTipText.
        Dim HireDate As DateTime = _
            CType(dataGridViewRow1.Cells("HireDate").Value, DateTime)
        e.ToolTipText += _
            String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}", _
                HireDate.Month.ToString(), HireDate.Day.ToString(), _
                HireDate.Year.ToString(), newLine, _
                dataGridViewRow1.Cells("Manager").Value.ToString())
    End If
End Sub

注解

CellToolTipTextNeeded事件时才会发生时DataGridView控制DataSource属性设置或其VirtualMode属性是trueThe CellToolTipTextNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true.

当处理CellToolTipTextNeeded处理程序中指定的工具提示文本显示事件,每当鼠标指针位于单元格和控件ShowCellToolTips属性值是trueWhen you handle the CellToolTipTextNeeded event, the ToolTip text you specify in the handler is shown whenever the mouse pointer is over a cell and the control ShowCellToolTips property value is true. CellToolTipTextNeeded事件非常有用,当你想要显示工具提示的当前状态或单元格的值。The CellToolTipTextNeeded event is useful when you want to display ToolTips determined by the current state or value of a cell.

CellToolTipTextNeeded时也会发生事件的值DataGridViewCell.ToolTipText检索属性,或者以编程方式或当鼠标指针进入单元格。The CellToolTipTextNeeded event also occurs whenever the value of the DataGridViewCell.ToolTipText property is retrieved, either programmatically or when the mouse pointer enters a cell.

可以使用ColumnIndexRowIndex属性来确定的状态或值的单元格,并使用此信息来设置ToolTipText属性。You can use the ColumnIndex and RowIndex properties to determine the state or value of a cell, and use this information to set the ToolTipText property. 此属性使用的单元格的值初始化ToolTipText属性,该事件值重写属性。This property is initialized with the value of the cell ToolTipText property, which the event value overrides.

处理CellToolTipTextNeeded事件时使用的大量数据,以避免设置单元格的性能损失ToolTipText多个单元格的值。Handle the CellToolTipTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell ToolTipText value for multiple cells. 有关详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

属性

ColumnIndex ColumnIndex ColumnIndex ColumnIndex

获取一个值,此值指示发生此事件的单元格的列索引。Gets a value indicating the column index of the cell that the event occurs for.

(Inherited from DataGridViewCellEventArgs)
RowIndex RowIndex RowIndex RowIndex

获取一个值,此值指示发生此事件的单元格的行索引。Gets a value indicating the row index of the cell that the event occurs for.

(Inherited from DataGridViewCellEventArgs)
ToolTipText ToolTipText ToolTipText ToolTipText

获取或设置工具提示文本。Gets or sets the ToolTip text.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅