DataGridView.InvalidateCell 方法

定义

使 DataGridView 中的某个单元格无效,并强制对它进行重新绘制。Invalidates a cell in the DataGridView, forcing it to be repainted.

重载

InvalidateCell(DataGridViewCell)

使 DataGridView 中的指定单元格无效,并强制对它进行重新绘制。Invalidates the specified cell of the DataGridView, forcing it to be repainted.

InvalidateCell(Int32, Int32)

使具有指定的行索引和列索引的单元格无效,并强制对它进行重新绘制。Invalidates the cell with the specified row and column indexes, forcing it to be repainted.

InvalidateCell(DataGridViewCell)

使 DataGridView 中的指定单元格无效,并强制对它进行重新绘制。Invalidates the specified cell of the DataGridView, forcing it to be repainted.

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

参数

dataGridViewCell
DataGridViewCell

要使之无效的 DataGridViewCellThe DataGridViewCell to invalidate.

异常

dataGridViewCell 不属于 DataGridViewdataGridViewCell does not belong to the DataGridView.

dataGridViewCellnulldataGridViewCell is null.

示例

下面的代码示例演示如何在自定义 DataGridViewCell 中使用此方法,当鼠标指针停留在其上时,将使用自定义边框进行绘制。The following code example illustrates how to use this method in a customized DataGridViewCell that is painted with a custom border when the mouse pointer rests on it. 在此示例中,当鼠标指针进入或离开该单元格时,该单元格将无效。In the example, the cell is invalidated when the mouse pointer enters or leaves it.

此代码是 [How 中提供的一个更大示例的一部分:通过扩展 no__t DataGridView 控件中 Windows 窗体的单元格和列的行为和外观,对其进行自定义 @。This code is part of a larger example available in How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance.

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

注解

此方法可用于在不会通常导致重绘单元格的条件下强制单元格重绘。This method is useful to force a cell repaint in conditions that would not normally cause a cell to be repainted. 例如,可以将此方法与自定义单元类型一起使用,该类型可更改其外观作为外部事件的结果。For example, you can use this method with a custom cell type that changes its appearance as a result of external events.

有关绘制和无效的详细信息,请参阅 InvalidateFor more information about painting and invalidation, see Invalidate.

另请参阅

InvalidateCell(Int32, Int32)

使具有指定的行索引和列索引的单元格无效,并强制对它进行重新绘制。Invalidates the cell with the specified row and column indexes, forcing it to be repainted.

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

要使其无效的单元格的列索引。The column index of the cell to invalidate.

rowIndex
Int32

要使其无效的单元格的行索引。The row index of the cell to invalidate.

异常

columnIndex 小于 -1 或大于控件中的列数减 1。columnIndex is less than -1 or greater than the number of columns in the control minus 1.

-or- rowIndex 小于 -1 或大于控件中的行数减 1。rowIndex is less than -1 or greater than the number of rows in the control minus 1.

示例

下面的代码示例演示如何在自定义单元格类型中使用此方法,当用户将鼠标指针悬停在其上方时更改单元格的外观。The following code example illustrates how to use this method in a custom cell type that changes a cell's appearance when the user rests the mouse pointer over it. 此示例摘自 [How 中提供的更大示例的一部分:通过扩展 no__t DataGridView 控件中 Windows 窗体的单元格和列的行为和外观,对其进行自定义 @。This example is part of a larger example available in How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance.

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

注解

此方法可用于在不会通常导致重绘单元格的条件下强制单元格重绘。This method is useful to force a cell repaint in conditions that would not normally cause a cell to be repainted. 例如,可以将此方法与自定义单元类型一起使用,该类型可更改其外观作为外部事件的结果。For example, you can use this method with a custom cell type that changes its appearance as a result of external events.

有关绘制和无效的详细信息,请参阅 InvalidateFor more information about painting and invalidation, see Invalidate.

另请参阅

适用于