DataGridViewRow Class


表示 DataGridView 控件中的行。Represents a row in a DataGridView control.

public ref class DataGridViewRow : System::Windows::Forms::DataGridViewBand
public class DataGridViewRow : System.Windows.Forms.DataGridViewBand
type DataGridViewRow = class
    inherit DataGridViewBand
Public Class DataGridViewRow
Inherits DataGridViewBand


下面的代码示例演示如何使用此类型。The following code example illustrates the use of this type. 此示例摘自如何:操作 Windows 窗体 DataGridView 控件中的行中提供的一个更大的示例。This example is part of a larger example available in How to: Manipulate Rows in the Windows Forms DataGridView Control.

// Set row labels.
void Button6_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )

   int rowNumber = 1;
   System::Collections::IEnumerator^ myEnum = safe_cast<System::Collections::IEnumerable^>(dataGridView->Rows)->GetEnumerator();
   while ( myEnum->MoveNext() )
      DataGridViewRow^ row = safe_cast<DataGridViewRow^>(myEnum->Current);
      if ( row->IsNewRow )
      row->HeaderCell->Value = String::Format( L"Row {0}", rowNumber );

      rowNumber = rowNumber + 1;

   dataGridView->AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode::AutoSizeToAllHeaders );

// Set row labels.
private void Button6_Click(object sender, System.EventArgs e)

    int rowNumber = 1;
    foreach (DataGridViewRow row in dataGridView.Rows)
        if (row.IsNewRow) continue;
        row.HeaderCell.Value = "Row " + rowNumber;
        rowNumber = rowNumber + 1;
' Set row labels.
Private Sub Button6_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button6.Click

    Dim rowNumber As Integer = 1
    For Each row As DataGridViewRow In dataGridView.Rows
        If row.IsNewRow Then Continue For
        row.HeaderCell.Value = "Row " & rowNumber
        rowNumber = rowNumber + 1
    dataGridView.AutoResizeRowHeadersWidth( _
End Sub


DataGridViewRow 类表示 DataGridView 控件中的一行。The DataGridViewRow class represents a row in a DataGridView control. 您可以通过控件的 RowsSelectedRows 集合来检索行。You can retrieve rows through the Rows and SelectedRows collections of the control.

DataGridViewColumn不同,DataGridViewRow 物理上包含该行中所有单元格的集合。Unlike a DataGridViewColumn, a DataGridViewRow physically contains a collection of all of the cells in that row. 可以通过 Cells 属性访问此集合。You can access this collection through the Cells property.

DataGridViewRow 类用于访问单个单元格元素,并用于调整行用户界面(UI)的外观和行为,例如高度和单元格样式。The DataGridViewRow class is used to access the individual cell elements, as well as to adjust the appearance and behavior of the row user interface (UI), such as height and cell style. 通常,您将希望控件中的所有行或大多数行共享相同的特性。Typically, you will want all rows or most rows in the control to share the same characteristics. 若要设置控件中所有行的单元格样式,请设置 DataGridView.RowsDefaultCellStyle 属性返回的对象的属性。To set cell styles for all rows in the control, set the properties of the object returned by the DataGridView.RowsDefaultCellStyle property. 若要为交替行设置样式,请使用 DataGridView.AlternatingRowsDefaultCellStyle 属性。To set styles for alternating rows, use the DataGridView.AlternatingRowsDefaultCellStyle property. 有关单元格样式的详细信息,请参阅Windows 窗体 DataGridView 控件中的单元格样式For more information about cell styles, see Cell Styles in the Windows Forms DataGridView Control. 你还可以使用 DataGridView.RowTemplate 属性来定义将用作添加到控件的所有行的基础的行。You can also use the DataGridView.RowTemplate property to define a row that will be used as a basis for all rows added to the control.

DataGridView 控件将尽可能跨多个数据行共享 DataGridViewRow 对象,以避免性能下降。The DataGridView control will share DataGridViewRow objects across multiple data rows whenever possible to avoid performance penalties. 除非您使用的数据量很大并且遇到性能问题,否则您通常可以忽略行共享。Unless you are working with large amounts of data and experiencing performance issues, you can typically ignore row sharing. 共享行由 Index 属性值-1 表示。A shared row is indicated by an Index property value of -1. DataGridViewRow 类的某些成员不能用于共享行,但您可以通过 DataGridViewRowCollection.Item[] 属性访问行来取消共享行。Some members of the DataGridViewRow class cannot be used with shared rows, but you can unshare a row by accessing it through the DataGridViewRowCollection.Item[] property. 还可以通过其他方式取消共享行。Rows can also become unshared in other ways. 若要在不取消共享行的情况下访问该行,请使用 DataGridViewRowCollection.SharedRow 方法。To access a row without unsharing it, use the DataGridViewRowCollection.SharedRow method. 处理大量数据时,应了解如何共享和取消共享行,以避免性能下降。When working with large amounts of data, you should be aware of how rows are shared and unshared to avoid performance penalties. 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Notes to Inheritors

当从 DataGridViewRow 派生并向派生类添加新属性时,请确保重写 Clone() 方法,以便在克隆操作过程中复制新属性。When you derive from DataGridViewRow and add new properties to the derived class, be sure to override the Clone() method to copy the new properties during cloning operations. 还应调用基类的 Clone() 方法,以便将基类的属性复制到新单元。You should also call the base class's Clone() method so that the properties of the base class are copied to the new cell.



在不使用模板的情况下初始化 DataGridViewRow 类的新实例。Initializes a new instance of the DataGridViewRow class without using a template.



获取分配给 DataGridViewRow.DataGridViewRowAccessibleObjectDataGridViewRowGets the DataGridViewRow.DataGridViewRowAccessibleObject assigned to the DataGridViewRow.


获取用于填充行的单元格集合。Gets the collection of cells that populate the row.


获取或设置行的快捷菜单。Gets or sets the shortcut menu for the row.


获取用于填充行的数据绑定对象。Gets the data-bound object that populated the row.


获取与此元素关联的 DataGridView 控件。Gets the DataGridView control associated with this element.

(Inherited from DataGridViewElement)

获取或设置行的默认样式,除非重写默认样式,否则将用它们来呈现行中的单元格。Gets or sets the default styles for the row, which are used to render cells in the row unless the styles are overridden.


获取或设置默认标题单元格的运行时类型。Gets or sets the run-time type of the default header cell.

(Inherited from DataGridViewBand)

获取一个值,指示此行是否显示在屏幕上。Gets a value indicating whether this row is displayed on the screen.


获取或设置行分隔符的高度(以像素为单位)。Gets or sets the height, in pixels, of the row divider.


获取或设置行级别错误的错误消息文本。Gets or sets the error message text for row-level errors.


获取或设置一个指示行是否已被冻结的值。Gets or sets a value indicating whether the row is frozen.


获取指示是否已设置 DefaultCellStyle 属性的值。Gets a value indicating whether the DefaultCellStyle property has been set.

(Inherited from DataGridViewBand)

获取或设置行的标头单元格。Gets or sets the row's header cell.


获取或设置 DataGridViewBand 的标题单元格。Gets or sets the header cell of the DataGridViewBand.

(Inherited from DataGridViewBand)

获取或设置行的当前高度。Gets or sets the current height of the row.


获取带区在 DataGridView 控件中的相对位置。Gets the relative position of the band within the DataGridView control.

(Inherited from DataGridViewBand)

获取行中生效的单元格样式。Gets the cell style in effect for the row.


获取一个值,指示行是否为用于新纪录的行。Gets a value indicating whether the row is the row for new records.


获取一个值,该值指示带区是否表示一个行。Gets a value indicating whether the band represents a row.

(Inherited from DataGridViewBand)

获取或设置行的最小高度。Gets or sets the minimum height of the row.


获取或设置一个值,指示行是否为只读。Gets or sets a value indicating whether the row is read-only.


获取或设置一个值,该值指示用户是否可以调整行大小,或指示该行为继承自 AllowUserToResizeRows 属性。Gets or sets a value indicating whether users can resize the row or indicating that the behavior is inherited from the AllowUserToResizeRows property.


获取或设置一个指示行是否已被选定的值。Gets or sets a value indicating whether the row is selected.


获取行的当前状态。Gets the current state of the row.


获取或设置包含与带区关联的数据的对象。Gets or sets the object that contains data to associate with the band.

(Inherited from DataGridViewBand)

获取或设置一个指示行是否可见的值。Gets or sets a value indicating whether the row is visible.


AdjustRowHeaderBorderStyle(DataGridViewAdvancedBorderStyle, DataGridViewAdvancedBorderStyle, Boolean, Boolean, Boolean, Boolean)

根据指定的条件修改输入行标头的边框样式。Modifies an input row header border style according to the specified criteria.


创建此行的精确副本。Creates an exact copy of this row.


DataGridViewRow 创建一个新的可访问对象。Creates a new accessible object for the DataGridViewRow.


根据所提供的 DataGridView 模板清除现有单元格并设置它们的模板。Clears the existing cells and sets their template according to the supplied DataGridView template.

CreateCells(DataGridView, Object[])

清除现有单元格并设置其模板和值。Clears the existing cells and sets their template and values.


根据此行构造新的单元格集合。Constructs a new collection of cells based on this row.


释放由 DataGridViewBand 使用的所有资源。Releases all resources used by the DataGridViewBand.

(Inherited from DataGridViewBand)

释放由 DataGridViewBand 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the DataGridViewBand and optionally releases the managed resources.

(Inherited from DataGridViewBand)
DrawFocus(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, DataGridViewCellStyle, Boolean)

围绕指定边界绘制聚焦框。Draws a focus rectangle around the specified bounds.


确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)

获取行的快捷菜单。Gets the shortcut menu for the row.


获取指定索引处的行的错误文本。Gets the error text for the row at the specified index.


作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetPreferredHeight(Int32, DataGridViewAutoSizeRowMode, Boolean)

根据指定的条件计算指定行的理想高度。Calculates the ideal height of the specified row based on the specified criteria.


返回一个值,指示行的当前状态。Returns a value indicating the current state of the row.


获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)

当带区与其他 DataGridView 关联时调用。Called when the band is associated with a different DataGridView.

(Inherited from DataGridViewBand)
Paint(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean)

绘制当前行。Paints the current row.

PaintCells(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean, DataGridViewPaintParts)

在当前行中绘制单元格。Paints the cells in the current row.

PaintHeader(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean, DataGridViewPaintParts)

绘制当前行的标头单元格。Paints the header cell of the current row.


引发 CellClick 事件。Raises the CellClick event.

(Inherited from DataGridViewElement)

引发 CellContentClick 事件。Raises the CellContentClick event.

(Inherited from DataGridViewElement)

引发 CellContentDoubleClick 事件。Raises the CellContentDoubleClick event.

(Inherited from DataGridViewElement)

引发 CellValueChanged 事件。Raises the CellValueChanged event.

(Inherited from DataGridViewElement)

引发 DataError 事件。Raises the DataError event.

(Inherited from DataGridViewElement)

引发 MouseWheel 事件。Raises the MouseWheel event.

(Inherited from DataGridViewElement)

设置行的单元格的值。Sets the values of the row's cells.


获取用于描述行的可读字符串。Gets a human-readable string that describes the row.

Applies to

See also