DataGridView.InvalidateCell Method (Int32, Int32)

Invalidates the cell with the specified row and column indexes, forcing it to be repainted.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void InvalidateCell(
	int columnIndex,
	int rowIndex
)

Parameters

columnIndex
Type: System.Int32

The column index of the cell to invalidate.

rowIndex
Type: System.Int32

The row index of the cell to invalidate.

ExceptionCondition
ArgumentOutOfRangeException

columnIndex is less than -1 or greater than the number of columns in the control minus 1.

-or-

rowIndex is less than -1 or greater than the number of rows in the control minus 1.

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.

For more information about painting and invalidation, see Invalidate.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft