DataGridView.CellToolTipTextNeeded Evento

Definición

Tiene lugar cuando se necesita el texto de la información sobre herramientas de una celda.

public:
 event System::Windows::Forms::DataGridViewCellToolTipTextNeededEventHandler ^ CellToolTipTextNeeded;
public event System.Windows.Forms.DataGridViewCellToolTipTextNeededEventHandler CellToolTipTextNeeded;
public event System.Windows.Forms.DataGridViewCellToolTipTextNeededEventHandler? CellToolTipTextNeeded;
member this.CellToolTipTextNeeded : System.Windows.Forms.DataGridViewCellToolTipTextNeededEventHandler 
Public Custom Event CellToolTipTextNeeded As DataGridViewCellToolTipTextNeededEventHandler 

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar un CellToolTipTextNeeded controlador de eventos para mostrar información de columnas ocultas en un control enlazado a DataGridView datos.

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

Comentarios

El CellToolTipTextNeeded evento solo se produce cuando se establece la propiedad de DataGridView control DataSource o su VirtualMode propiedad es true.

Al controlar el CellToolTipTextNeeded evento, se muestra el texto de información sobre herramientas que especifique en el controlador si el valor de la ShowCellToolTips propiedad es true y el puntero del mouse se encuentra sobre la celda o el usuario navega a la celda mediante el teclado. El CellToolTipTextNeeded evento es útil cuando desea mostrar información sobre herramientas determinada por el estado actual o el valor de una celda.

El CellToolTipTextNeeded evento también se produce cada vez que se recupera el valor de la DataGridViewCell.ToolTipText propiedad, ya sea mediante programación o cuando el usuario navega a la celda con el mouse o el teclado.

Puede usar las DataGridViewCellEventArgs.ColumnIndex propiedades y RowIndex para determinar el estado o el valor de una celda y usar esta información para cambiar o modificar la DataGridViewCellToolTipTextNeededEventArgs.ToolTipText propiedad. Esta propiedad se inicializa con el valor de la propiedad cell ToolTipText , que el valor del evento invalida.

Controle el CellToolTipTextNeeded evento al trabajar con grandes cantidades de datos para evitar las penalizaciones de rendimiento de establecer el valor de celda ToolTipText para varias celdas. Para obtener más información, consulte Procedimientos recomendados para ajustar la escala del control DataGridView en Windows Forms.

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Consulte también