'C# Invalid expression term '+='

for (int i = 0; i <= dataGridView1.Rows.Count; i++)
{
    string point_value += dataGridView1.Rows[i].Cells[1].Value + "|"; 
}

Does anyone know why I am getting this error

Invalid expression term '+='

for this code?



Solution 1:[1]

you create new variable with in your for loop that the reason

if you need to store data each loop you should create variable outside loop

like this

string point_value = "";
for(int i=0; i < dataGridView1.Rows.Count; i++)
    {
        point_value += dataGridView1.Rows[i].Cells[1].Value + "|"; 
    }

for more information about loop pattern here

(edited to avoid empty value and out of range from DiplomacyNotWar's commented)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1