DataGridViewCellEventHandler 委托

定义

表示将处理与单元格和行操作相关的 DataGridView 事件的方法。

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

参数

sender
Object

事件源。

示例

下面的代码示例演示如何使用 CellMouseEnterCellMouseLeave 事件处理程序来确定是否可以单击单元格。 该示例更新每个 ToolTipText 以播发当前图像布局。 此代码是How to: Work with Image Columns in the Windows 窗体 DataGridView 控件中显示的较大示例的一部分。

void dataGridView1_CellMouseEnter( Object^ sender, DataGridViewCellEventArgs^ e )
{
   Bitmap^ markingUnderMouse = dynamic_cast<Bitmap^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]->Value);
   if ( markingUnderMouse == blank )
   {
      dataGridView1->Cursor = Cursors::Default;
   }
   else
   if ( markingUnderMouse == o || markingUnderMouse == x )
   {
      dataGridView1->Cursor = Cursors::No;
      ToolTip(e,true);
   }
}

void ToolTip( DataGridViewCellEventArgs^ e, bool showTip )
{
   DataGridViewImageCell^ cell = dynamic_cast<DataGridViewImageCell^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]);
   DataGridViewImageColumn^ imageColumn = dynamic_cast<DataGridViewImageColumn^>(dataGridView1->Columns[ cell->ColumnIndex ]);
   if ( showTip )
         cell->ToolTipText = imageColumn->Description;
   else
   {
      cell->ToolTipText = String::Empty;
   }
}

void dataGridView1_CellMouseLeave( Object^ sender, DataGridViewCellEventArgs^ e )
{
   ToolTip( e, false );
   dataGridView1->Cursor = Cursors::Default;
}
private void dataGridView1_CellMouseEnter(object sender,
    DataGridViewCellEventArgs e)
{
    Bitmap markingUnderMouse = (Bitmap)dataGridView1.
           Rows[e.RowIndex].
           Cells[e.ColumnIndex].Value;

    if (markingUnderMouse == blank)
    {
        dataGridView1.Cursor = Cursors.Default;
    }
    else if (markingUnderMouse == o || markingUnderMouse == x)
    {
        dataGridView1.Cursor = Cursors.No;
        ToolTip(e, true);
    }
}

private void ToolTip(DataGridViewCellEventArgs e, bool showTip)
{
    DataGridViewImageCell cell = (DataGridViewImageCell)
        dataGridView1
        .Rows[e.RowIndex].Cells[e.ColumnIndex];
    DataGridViewImageColumn imageColumn =
        (DataGridViewImageColumn)
        dataGridView1.Columns[cell.ColumnIndex];

    if (showTip)
    {
        cell.ToolTipText = imageColumn.Description;
    }
    else { cell.ToolTipText = String.Empty; }
}

private void dataGridView1_CellMouseLeave(object sender,
    DataGridViewCellEventArgs e)
{
    ToolTip(e, false);
    dataGridView1.Cursor = Cursors.Default;
}
Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellMouseEnter

    Dim markingUnderMouse As Bitmap = _
        CType(dataGridView1.Rows(e.RowIndex). _
            Cells(e.ColumnIndex).Value, Bitmap)

    If markingUnderMouse Is blank Then
        dataGridView1.Cursor = Cursors.Default
    ElseIf markingUnderMouse Is o OrElse markingUnderMouse Is x Then
        dataGridView1.Cursor = Cursors.No
        ToolTip(e)
    End If
End Sub

Private Sub ToolTip( _
    ByVal e As DataGridViewCellEventArgs)

    Dim cell As DataGridViewImageCell = _
        CType(dataGridView1.Rows(e.RowIndex). _
        Cells(e.ColumnIndex), DataGridViewImageCell)
    Dim imageColumn As DataGridViewImageColumn = _
        CType(dataGridView1.Columns(cell.ColumnIndex), _
        DataGridViewImageColumn)

    cell.ToolTipText = imageColumn.Description
End Sub

Private Sub dataGridView1_CellMouseLeave(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellMouseLeave

    dataGridView1.Cursor = Cursors.Default
End Sub

注解

DataGridViewCellEventHandler 处理以下 DataGridView 事件:

创建 DataGridViewCellEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅