System.Windows.Forms.DataGridView.Rows Property

Gets a collection that contains all the rows in the System.Windows.Forms.DataGridView control.

Syntax

[System.ComponentModel.Browsable(false)]
public DataGridViewRowCollection Rows { get; }

Value

Documentation for this section has not yet been entered.

Remarks

You can use the DataGridView.Rows collection to manually populate a System.Windows.Forms.DataGridView control instead of binding it to a data source. The following example shows you how to manually add and insert rows. This example assumes that you have added four System.Windows.Forms.DataGridViewTextBoxColumn instances to the control's DataGridView.Columns collection.

[Visual Basic]

Example

Me.dataGridView1.Rows.Add("five", "six", "seven", "eight")
Me.dataGridView1.Rows.Insert(0, "one", "two", "three", "four")

[C#]

Example

this.dataGridView1.Rows.Add("five", "six", "seven", "eight");this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");

For a detailed example that programmatically populates an unbound System.Windows.Forms.DataGridView control, see the Example section.

Rows include style information in addition to cell values. For this reason, you might want to add or insert rows based on existing rows that you have already styled. You can do this using the DataGridViewRowCollection.AddCopy(int), DataGridViewRowCollection.AddCopies(int, int), DataGridViewRowCollection.InsertCopy(int, int), and DataGridViewRowCollection.InsertCopies(int, int, int) methods.

You can also use the DataGridView.Rows collection to modify the values in the control or to remove rows. You can modify values or remove rows regardless of whether the control is bound to an external data source. If there is a data source, the changes are made directly to the data source. You may still need to push the data source updates to a remote database, however. For more information, see How to: Bind Data to the Windows Forms DataGridView Control.

The following example shows you how to modify cell values programmatically.

[Visual Basic]

Example

' Modify the value in the first cell of the second row.
Me.dataGridView1.Rows[1].Cells[0].Value = "new value"

' The previous line is equivalent to the following line.
Me.dataGridView1[0, 1].Value = "new value"

[C#]

Example

// Modify the value in the first cell of the second row.
this.dataGridView1.Rows[1].Cells[0].Value = "new value";

// The previous line is equivalent to the following line.
this.dataGridView1[0, 1].Value = "new value";

In addition to the standard collection capabilities, you can use the DataGridView.Rows collection to retrieve information about rows. Use the DataGridViewRowCollection.GetRowState(int) method to determine the state of a particular row. Use the DataGridViewRowCollection.GetRowCount(DataGridViewElementStates) and DataGridViewRowCollection.GetRowsHeight(DataGridViewElementStates) methods to determine the number of rows or the combined height of rows in a particular state. To retrieve the index of a row with a particular state, use the DataGridViewRowCollection.GetFirstRow(DataGridViewElementStates), DataGridViewRowCollection.GetLastRow(DataGridViewElementStates), DataGridViewRowCollection.GetNextRow(int, DataGridViewElementStates), and DataGridViewRowCollection.GetPreviousRow(int, DataGridViewElementStates) methods.

The following example shows you how to retrieve the index of the first selected row, and then use it to programmatically delete the row.

[Visual Basic]

Example

Dim rowToDelete As Int32 = Me.dataGridView1.Rows.GetFirstRow( _
    DataGridViewElementStates.Selected)
If rowToDelete > -1 Then
    Me.dataGridView1.Rows.RemoveAt(rowToDelete)
End If

[C#]

Example

Int32 rowToDelete = this.dataGridView1.Rows.GetFirstRow(
    DataGridViewElementStates.Selected);
if (rowToDelete > -1)
{
    this.dataGridView1.Rows.RemoveAt(rowToDelete);
}

To improve performance, the System.Windows.Forms.DataGridViewRowCollection returned by the DataGridView.Rows property can include shared and unshared rows. Shared rows share memory to reduce the cost of a large record set. If your record set is very large, you should be careful to keep the rows shared as much as possible when accessing the DataGridView.Rows property.

For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0