1

I am trying to store the resulting strings together (combine them) into a single string.

for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    string pedido = dataGridView1.Rows[i].Cells[2].Value.ToString();
}
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
RenĂª Lima
  • 27
  • 1
  • 6

2 Answers2

5

I would use a StringBuilder for this rather than a String.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataGridView1.Rows.Count; ++i){
    sb.Append(dataGridView1.Rows[i].Cells[2].Value.ToString());
 }

string pedido = sb.ToString();

Strings are immutable, so they cannot be changed. If you keep appending values to the same string, you may run into some performance problems creating and destroying the values all the time. StringBuilder will let you keep appending without all that overhead.

mgnoonan
  • 7,060
  • 5
  • 24
  • 27
2

Something like this with Linq might work:

String.Join(",", (from a in dataGridView1.AsEnumerable() select a.Cells[2].Value.ToString()).ToList())
Chris Ray
  • 4,833
  • 2
  • 20
  • 19