Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

DataGridView.CellToolTipTextNeeded événement

 

Date de publication : novembre 2016

Se produit lorsque le texte ToolTip d'une cellule est nécessaire.

Espace de noms:   System.Windows.Forms
Assembly:  System.Windows.Forms (dans System.Windows.Forms.dll)

public event DataGridViewCellToolTipTextNeededEventHandler CellToolTipTextNeeded

The E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event occurs only when the T:System.Windows.Forms.DataGridView control P:System.Windows.Forms.DataGridView.DataSource property is set or its P:System.Windows.Forms.DataGridView.VirtualMode property is true.

When you handle the E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event, the ToolTip text you specify in the handler is shown whenever the mouse pointer is over a cell and the control P:System.Windows.Forms.DataGridView.ShowCellToolTips property value is true. The E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event is useful when you want to display ToolTips determined by the current state or value of a cell.

The E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event also occurs whenever the value of the P:System.Windows.Forms.DataGridViewCell.ToolTipText property is retrieved, either programmatically or when the mouse pointer enters a cell.

You can use the P:System.Windows.Forms.DataGridViewCellEventArgs.ColumnIndex and P:System.Windows.Forms.DataGridViewCellEventArgs.RowIndex properties to determine the state or value of a cell, and use this information to change or modify the P:System.Windows.Forms.DataGridViewCellToolTipTextNeededEventArgs.ToolTipText property. This property is initialized with the value of the cell P:System.Windows.Forms.DataGridViewCell.ToolTipText property, which the event value overrides.

Handle the E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell P:System.Windows.Forms.DataGridViewCell.ToolTipText value for multiple cells. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

For more information about handling events, see NIB: Consuming Events.

The following code example demonstrates how to use a E:System.Windows.Forms.DataGridView.CellToolTipTextNeeded event handler to display information from hidden columns in a data-bound T:System.Windows.Forms.DataGridView control.

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());
    }
}

.NET Framework
Disponible depuis 2.0
Retour au début
Afficher: