Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have an excel file and need to get certaain data from this file and show it in a datagridview but i have a problem. I need to clone the cell value of the previous row of my datagridview if row[i].itemarray[0] is null.

What I have tried:

for (int i = 1; i < dt.Rows.Count; i++)
{
 if (d.Rows[i].ItemArray[0].ToString() == "" || d.Rows[i].ItemArray[0].ToString() == null)
    {
          d.Rows[i].ItemArray[0] = d.Rows[i - 1].ItemArray[0];
     }
}
Posted
Updated 24-Dec-20 4:35am
Comments
Richard MacCutchan 24-Dec-20 6:24am    
And? What is the question?

1 solution

If you store the value of each iteration at the end of the loop, then you can access it at the beginning of the next loop (if you need it), without having to "index backwards".

Obviously, the first time around, there is no "previous value", so you would initialize the "previous" variable to null (or something) and test for that (indicating it's the "first loop").

Your current solution doesn't handle the "first loop" properly: you're indexing into "nowhere".

And you're first row is at index "0", not "1".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900