Access GridRow in a Worksheet

Iterate over the rows

Best Practices: If we want to access all the rows in the worksheet one by one, we can use iterators to traverse the existing rows. This will save memory.

Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Accessing a row using iterators
GridCells cells = sheet.Cells;
foreach (GridRow row in cells.Rows)
{
    Console.WriteLine(row.Index + " " + row.Height);
}

Compare the code below; this will create all the row objects regardless of whether they are null, thus causing memory issues, so please do not use this approach.

Worksheet sheet = gridDesktop1.GetActiveWorksheet();
GridCells cells = sheet.Cells;
for (int r = 0; r < sheet.RowsCount; r++)
{
    GridRow row = cells.Rows[r];
    Console.WriteLine(row.Index + " " + row.Height);
}

However, you can use the CheckRow method to check if the row is empty.

Worksheet sheet = gridDesktop1.GetActiveWorksheet();
GridCells cells = sheet.Cells;
for (int r = 0; r < sheet.RowsCount; r++)
{
    GridRow row = cells.CheckRow(r);
    if (row == null)
    {
        Console.WriteLine("the row is empty:" + r);
    }
    else
    {
        Console.WriteLine(row.Index + " " + row.Height);
    }
}
}