DataGridView.InvalidateCell 方法

定义

使 DataGridView 中的某个单元格无效,并强制对它进行重新绘制。

重载

InvalidateCell(DataGridViewCell)

使 DataGridView 中的指定单元格无效,并强制对它进行重新绘制。

InvalidateCell(Int32, Int32)

使具有指定的行索引和列索引的单元格无效,并强制对它进行重新绘制。

InvalidateCell(DataGridViewCell)

使 DataGridView 中的指定单元格无效,并强制对它进行重新绘制。

public:
 void InvalidateCell(System::Windows::Forms::DataGridViewCell ^ dataGridViewCell);
public void InvalidateCell (System.Windows.Forms.DataGridViewCell dataGridViewCell);
member this.InvalidateCell : System.Windows.Forms.DataGridViewCell -> unit
Public Sub InvalidateCell (dataGridViewCell As DataGridViewCell)

参数

dataGridViewCell
DataGridViewCell

要使之无效的 DataGridViewCell

例外

dataGridViewCell 不属于 DataGridView

dataGridViewCellnull

示例

下面的代码示例演示如何在鼠标指针位于自定义边框的自定义 DataGridViewCell 中使用此方法。 在此示例中,当鼠标指针进入或离开单元格时,单元格将失效。

此代码是有关如何:通过扩展其行为和外观来自定义 dataGridView 控件中Windows 窗体单元格和列的更大示例的一部分。

// Force the cell to repaint itself when the mouse pointer enters it.
protected override void OnMouseEnter(int rowIndex)
{
    this.DataGridView.InvalidateCell(this);
}

// Force the cell to repaint itself when the mouse pointer leaves it.
protected override void OnMouseLeave(int rowIndex)
{
    this.DataGridView.InvalidateCell(this);
}
' Force the cell to repaint itself when the mouse pointer enters it.
Protected Overrides Sub OnMouseEnter(ByVal rowIndex As Integer)
    Me.DataGridView.InvalidateCell(Me)
End Sub

' Force the cell to repaint itself when the mouse pointer leaves it.
Protected Overrides Sub OnMouseLeave(ByVal rowIndex As Integer)
    Me.DataGridView.InvalidateCell(Me)
End Sub

注解

此方法可用于强制单元格重新绘制在通常不会导致重新绘制单元格的情况下。 例如,可以将此方法与自定义单元格类型结合使用,该类型会由于外部事件而更改其外观。

有关绘制和无效的详细信息,请参阅 Invalidate

另请参阅

适用于

InvalidateCell(Int32, Int32)

使具有指定的行索引和列索引的单元格无效,并强制对它进行重新绘制。

public:
 void InvalidateCell(int columnIndex, int rowIndex);
public void InvalidateCell (int columnIndex, int rowIndex);
member this.InvalidateCell : int * int -> unit
Public Sub InvalidateCell (columnIndex As Integer, rowIndex As Integer)

参数

columnIndex
Int32

要使其无效的单元格的列索引。

rowIndex
Int32

要使其无效的单元格的行索引。

例外

columnIndex 小于 -1 或大于控件中的列数减 1。

  • 或 - rowIndex 小于 -1 或大于控件中的行数减 1。

示例

下面的代码示例演示如何在自定义单元格类型中使用此方法,当用户将鼠标指针悬停在单元格上方时更改单元格的外观。 此示例是“如何:通过扩展其行为和外观”来自定义 dataGridView 控件中Windows 窗体单元格和列的更大示例的一部分。

// Force the cell to repaint itself when the mouse pointer enters it.
protected override void OnMouseEnter(int rowIndex)
{
    this.DataGridView.InvalidateCell(this);
}

// Force the cell to repaint itself when the mouse pointer leaves it.
protected override void OnMouseLeave(int rowIndex)
{
    this.DataGridView.InvalidateCell(this);
}
' Force the cell to repaint itself when the mouse pointer enters it.
Protected Overrides Sub OnMouseEnter(ByVal rowIndex As Integer)
    Me.DataGridView.InvalidateCell(Me)
End Sub

' Force the cell to repaint itself when the mouse pointer leaves it.
Protected Overrides Sub OnMouseLeave(ByVal rowIndex As Integer)
    Me.DataGridView.InvalidateCell(Me)
End Sub

注解

此方法可用于强制单元格重新绘制在通常不会导致重新绘制单元格的情况下。 例如,可以将此方法与自定义单元格类型结合使用,该类型会由于外部事件而更改其外观。

有关绘制和无效的详细信息,请参阅 Invalidate

另请参阅

适用于