UsedRange.RowCount is not updated after an insert

485 Views Asked by At

I'm trying to write an Extension methods that adds a generic Item T to the workbook, I've got problem since the UsedRange.RowCount is not incremented after InsertMethod is called

    public static RowItem<T> AddItem<T>(this SpreadsheetGear.IWorksheet worksheet, T item) where T : class
    {
        int currentRow = worksheet.UsedRange.RowCount;
        //int currentRow = worksheet.UsedRange.RowCount;
        RowItem<T> newItem = new RowItem<T>
        {
            Item = item,
            RowIndex = currentRow
        };

        var reflected = item.GetType().GetProperties();

        for (int i = 0; i < reflected.Length; i++)
        {
            object value = reflected[i].GetValue(item);
            worksheet.Cells[currentRow, i].Value = value;
        }

        worksheet.UsedRange.Insert(SpreadsheetGear.InsertShiftDirection.Down);
        worksheet.WorkbookSet.CalculateFull();

        return newItem;
    }

 public static IEnumerable<RowItem<T>> AddItems<T>(this SpreadsheetGear.IWorksheet worksheet, IEnumerable<T> items) where T : class
    {
        var lst = new List<RowItem<T>>();

        foreach (var item in items)
        {
            var newItem = AddItem<T>(worksheet, item);

            lst.Add(newItem);
        }

        return lst;
    }

It's always 1 ...what am I doing wrong? my dummy class is

public class Dummy
{
    public string Desciption { get; set; }
    public double Value { get; set; }
    public DateTime Data { get; set; }
}

And I add items as

  using (var _ = new WorkbookViewLock(workbookView1))
  {
     var worksheet = workbookView1.ActiveSheet.Workbook.Worksheets[0];

     worksheet.AddItem<Dummy>(dummy);
     worksheet.AddItem<Dummy>(dummy2);
  }
1

There are 1 best solutions below

4
On

If you are starting out with a blank worksheet (are you?), then it would make sense that the first two checks you make to worksheet.UsedRange.RowCount in your code would have a value of 1.

This is because the UsedRange of a blank worksheet will always be A1, which would correspond to a UsedRange.RowCount value of 1. That accounts for a value of 1 for your first AddItem(...) call.

The second AddItem(...) call is now looking at a worksheet that is populated with data, but still only 1 row since you've only added a single Dummy object at this point.

If you were to add a third Dummy object, you would see that the UsedRange increments to a value of 2.

FYI: You might have an additional issue with with your worksheet.UsedRange.Insert(...) line, since this will insert the number of rows that UsedRange currently consists of. It seems to me that if you are adding just a single Dummy object with this extension method, you should only insert, at most, one row...and that depends on where you want each new Dummy row to get added to the worksheet--the top or bottom of the used range. If you are inserting the new Dummy object at the top of the UsedRange, you should only apply Insert(...) on the top row of the UsedRange. If you are inserting the new Dummy object at the bottom of the UsedRange, no Insert(...) call is necessary at all since there's nothing below the UsedRange to shift down.