VSTO: insert one row into a 1-row range

  • Thread starter Thread starter jj_online
  • Start date Start date
J

jj_online

I have a programming written in C#, trying to insert a new row into a
range. If the range has two rows or more than two rows, all work just
fine. However, if the range only has one row, the insert doesn't work
quite right. The insert seems working (visually the worksheet has one
more row), but the range is not updated. The code looks like the
following:

NamedRange nr = Controls.AddNamedRange("A1", Type.Missing);
Range r = nr.Cells[1,1].EntireRow;
r.Insert(XlInsertShiftDirection.xlShiftDown, Type.Missing);

After above code, I would expect that the namedrange nr has two rows.
But it still has one row even a new row is actually insert into
worksheet.

However, if the namedrange has two rows or more, after executing above
codes, nr.Rows.Count will be incremented.

What can be wrong in my code? Does it mean NamedRange doesn't work for
1 row range?

Thanks for reading and help!

JJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top