Редагувати

Поділитися через


DataGridViewCellToolTipTextNeededEventHandler Delegate

Definition

Represents the method that will handle the CellToolTipTextNeeded event of a DataGridView.

public delegate void DataGridViewCellToolTipTextNeededEventHandler(System::Object ^ sender, DataGridViewCellToolTipTextNeededEventArgs ^ e);
public delegate void DataGridViewCellToolTipTextNeededEventHandler(object sender, DataGridViewCellToolTipTextNeededEventArgs e);
public delegate void DataGridViewCellToolTipTextNeededEventHandler(object? sender, DataGridViewCellToolTipTextNeededEventArgs e);
type DataGridViewCellToolTipTextNeededEventHandler = delegate of obj * DataGridViewCellToolTipTextNeededEventArgs -> unit
Public Delegate Sub DataGridViewCellToolTipTextNeededEventHandler(sender As Object, e As DataGridViewCellToolTipTextNeededEventArgs)

Parameters

sender
Object

The source of the event.

Examples

The following code example uses ToolTips to display additional information about cell contents in the ReportsTo column.

DataTable^ Populate( String^ query, bool resetUnsharedCounter )
{
    if ( resetUnsharedCounter )
    {
        ResetCounter();
    }


    // Alter the data source as necessary
    SqlDataAdapter^ adapter = gcnew SqlDataAdapter( query,
        gcnew SqlConnection( L"Integrated Security=SSPI;Persist Security Info=False;"
        L"Initial Catalog=Northwind;Data Source= localhost" ) );
    DataTable^ table = gcnew DataTable;
    adapter->Fill( table );
    return table;
}

Label^ count;
int unsharedRowCounter;
void ResetCounter()
{
    unsharedRowCounter = 0;
    count->Text = unsharedRowCounter.ToString();
}

void dataGridView1_CellToolTipTextNeeded( Object^ /*sender*/,
    DataGridViewCellToolTipTextNeededEventArgs^ e )
{
    if ( theCellImHoveringOver->ColumnIndex == dataGridView1->Columns[ L"ReportsTo" ]->Index && theCellImHoveringOver->RowIndex > -1 )
    {
        String^ reportsTo = dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value->ToString();
        if ( reportsTo->Equals( L"" ) )
        {
            e->ToolTipText = L"The buck stops here!";
        }
        else
        {
            DataTable^ table = Populate( String::Format( L"select firstname, lastname from employees where employeeid = '{0}'",
                dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value ), false );
            e->ToolTipText = String::Format( L"Reports to {0} {1}", table->Rows[ 0 ]->ItemArray[ 0 ], table->Rows[ 0 ]->ItemArray[ 1 ] );
        }
    }
}
private DataTable Populate(string query, bool resetUnsharedCounter)
{
    if (resetUnsharedCounter)
    {
        ResetCounter();
    }

    // Alter the data source as necessary
    SqlDataAdapter adapter = new SqlDataAdapter(query,
        new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost"));

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

private Label count = new Label();
private int unsharedRowCounter;

private void ResetCounter()
{
    unsharedRowCounter = 0;
    count.Text = unsharedRowCounter.ToString();
}

private void dataGridView1_CellToolTipTextNeeded(object sender,
    DataGridViewCellToolTipTextNeededEventArgs e)
{

    if (theCellImHoveringOver.ColumnIndex == dataGridView1.Columns["ReportsTo"].Index &&
        theCellImHoveringOver.RowIndex > -1)
    {

        string reportsTo = dataGridView1.Rows[theCellImHoveringOver.RowIndex].
            Cells[theCellImHoveringOver.ColumnIndex].Value.ToString();

        if (String.IsNullOrEmpty(reportsTo))
        {
            e.ToolTipText = "The buck stops here!";
        }
        else
        {
            DataTable table = Populate(
                "select firstname, lastname from employees where employeeid = '" +
                dataGridView1.Rows[theCellImHoveringOver.RowIndex].
                Cells[theCellImHoveringOver.ColumnIndex].Value.ToString() +
                "'", false);

            e.ToolTipText = "Reports to " + table.Rows[0].ItemArray[0] + " " +
                table.Rows[0].ItemArray[1];
        }
    }
}
Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable

    If resetUnsharedCounter Then
        ResetCounter()
    End If

    ' Alter the data source as necessary
    Dim adapter As New SqlDataAdapter(query, _
        New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _
        "Initial Catalog=Northwind;Data Source=localhost"))

    Dim table As New DataTable()
    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)
    Return table
End Function

Private count As New Label()
Private unsharedRowCounter As Integer

Private Sub ResetCounter()
    unsharedRowCounter = 0
    count.Text = unsharedRowCounter.ToString()
End Sub

Private Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _
    ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _
    Handles dataGridView1.CellToolTipTextNeeded

    If theCellImHoveringOver.ColumnIndex = dataGridView1.Columns("ReportsTo").Index AndAlso _
            theCellImHoveringOver.RowIndex > -1 Then

        Dim reportsTo As String = dataGridView1.Rows(theCellImHoveringOver.RowIndex). _
            Cells(theCellImHoveringOver.ColumnIndex).Value.ToString()
        If String.IsNullOrEmpty(reportsTo) Then
            e.ToolTipText = "The buck stops here!"
        Else
            Dim table As DataTable = Populate( _
                "select firstname, lastname from employees where employeeid = '" & _
                dataGridView1.Rows(theCellImHoveringOver.RowIndex). _
                Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _
                "'", False)

            e.ToolTipText = "Reports to " & table.Rows(0).Item(0).ToString() & " " & table.Rows(0).Item(1).ToString()
        End If
    End If
End Sub

Remarks

The CellToolTipTextNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true.

When 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. The CellToolTipTextNeeded event is useful when you want to display ToolTips determined by the current state or value of a cell.

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.

You can use the DataGridViewCellEventArgs.ColumnIndex and RowIndex properties to determine the state or value of a cell, and use this information to set the ToolTipText property. This property is initialized with the value of the cell ToolTipText property, which the event value overrides.

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. 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.

When you create a DataGridViewCellToolTipTextNeededEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Handling and Raising Events.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

See also