This documentation is archived and is not being maintained.

DataGridView.Rows Property

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

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

public DataGridViewRowCollection Rows { get; }

Property Value

Type: System.Windows.Forms.DataGridViewRowCollection
A DataGridViewRowCollection that contains all the rows in the DataGridView.

You can use the Rows collection to manually populate a 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 DataGridViewTextBoxColumn instances to the control's Columns collection.

[Visual Basic]

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


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 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 AddCopy, AddCopies, InsertCopy, and InsertCopies methods.

You can also use the 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]

' 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"


// 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 Rows collection to retrieve information about rows. Use the GetRowState method to determine the state of a particular row. Use the GetRowCount and GetRowsHeight 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 GetFirstRow, GetLastRow, GetNextRow, and GetPreviousRow 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]

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


Int32 rowToDelete = this.dataGridView1.Rows.GetFirstRow(
if (rowToDelete > -1)

To improve performance, the DataGridViewRowCollection returned by the 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 Rows property.

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

The following code example demonstrates how to create an unbound DataGridView; set the ColumnHeadersVisible, ColumnHeadersDefaultCellStyle, and ColumnCount properties; and use the Rows and Columns properties. It also demonstrates how to use a version of the AutoResizeColumnHeadersHeight and AutoResizeRows methods to properly size the column headers and the rows. To run this example, paste the following code into a form that contains a DataGridView named dataGridView1 and a button named Button1, and then call the InitializeDataGridView method from the form's constructor or Load event handler. Ensure all events are connected with their event handlers.

    private void InitializeDataGridView()
        // Create an unbound DataGridView by declaring a column count.
        dataGridView1.ColumnCount = 4;
        dataGridView1.ColumnHeadersVisible = true;

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

        columnHeaderStyle.BackColor = Color.Beige;
        columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
        dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

        // Set the column header names.
        dataGridView1.Columns[0].Name = "Recipe";
        dataGridView1.Columns[1].Name = "Category";
        dataGridView1.Columns[2].Name = "Main Ingredients";
        dataGridView1.Columns[3].Name = "Rating";

        // Populate the rows.
        string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef",
            "**" };
        string[] row2 = new string[] { "Key Lime Pie", "Dessert", 
            "lime juice, evaporated milk", "****" };
        string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish", 
            "pork chops, salsa, orange juice", "****" };
        string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad", 
            "black beans, brown rice", "****" };
        string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert", 
            "cream cheese", "***" };
        string[] row6 = new string[] { "Black Bean Dip", "Appetizer", 
            "black beans, sour cream", "***" };
        object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };

        foreach (string[] rowArray in rows)

    private void button1_Click(object sender, System.EventArgs e)
        // Resize the height of the column headers. 

        // Resize all the row heights to fit the contents of all non-header cells.

    private void InitializeContextMenu()
        // Create the menu item.
        ToolStripMenuItem getRecipe = new ToolStripMenuItem("Search for recipe", null,
            new System.EventHandler(ShortcutMenuClick));

        // Add the menu item to the shortcut menu.
        ContextMenuStrip recipeMenu = new ContextMenuStrip();

        // Set the shortcut menu for the first column.
        dataGridView1.Columns[0].ContextMenuStrip = recipeMenu;
        dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);

    private DataGridViewCell clickedCell;

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
	// If the user right-clicks a cell, store it for use by the shortcut menu.
        if (e.Button == MouseButtons.Right)
            DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
            if (hit.Type == DataGridViewHitTestType.Cell)
                clickedCell =

    private void ShortcutMenuClick(object sender, System.EventArgs e)
        if (clickedCell != null)
            //Retrieve the recipe name.
            string recipeName = (string)clickedCell.Value;

            //Search for the recipe.
                "" + recipeName);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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