DataGridView.InvalidateRow(Int32) 方法

定义

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

public:
 void InvalidateRow(int rowIndex);
public void InvalidateRow (int rowIndex);
member this.InvalidateRow : int -> unit
Public Sub InvalidateRow (rowIndex As Integer)

参数

rowIndex
Int32

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

异常

rowIndex 不在 0 到行数减 1 的有效范围内。rowIndex is not in the valid range of 0 to the number of rows minus 1.

示例

下面的代码示例演示如何在行绘制方案中使用 InvalidateRow 方法。The following code example illustrates how to use the InvalidateRow method in a row-painting scenario. 在此示例中,如果当前单元格发生更改,则行失效,并强制该行自行重绘。In the example, the row is invalidated when the current cell changes, forcing the row to repaint itself.

此代码是 [How 中提供的一个更大示例的一部分:自定义 Windows 窗体 DataGridView 控件 @ no__t 中的行的外观。This code is part of a larger example available in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// Forces the row to repaint itself when the user changes the 
// current cell. This is necessary to refresh the focus rectangle.
void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
    if (oldRowIndex != -1)
    {
        this.dataGridView1.InvalidateRow(oldRowIndex);
    }
    oldRowIndex = this.dataGridView1.CurrentCellAddress.Y;
}
' Forces the row to repaint itself when the user changes the 
' current cell. This is necessary to refresh the focus rectangle.
Sub dataGridView1_CurrentCellChanged(ByVal sender As Object, _
    ByVal e As EventArgs) Handles dataGridView1.CurrentCellChanged

    If oldRowIndex <> -1 Then
        Me.dataGridView1.InvalidateRow(oldRowIndex)
    End If
    oldRowIndex = Me.dataGridView1.CurrentCellAddress.Y

End Sub

注解

使用此方法强制行重绘自身。Use this method to force a row to repaint itself. 此方法可用于处理 RowPrePaint 或 @no__t 1 事件的所有者描述方案。This method is useful in owner-drawing scenarios where you handle the RowPrePaint or RowPostPaint events.

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

适用于

另请参阅