Well if you want to get to row i
, you need to change your check from
&& dtNew.Rows.Count > 0
to
&& dtNew.Rows.Count > i
Currently you're only checking whether there are any rows - i.e. whether dtNew.Rows[0]
is valid.
(Do you definitely need to check for dtNew
being null
? Is that a valid program state? Likewise is it valid for the row to exist but column 0 to not be populated? You may be able to make your code much simpler.)