[C#] Looping Through Datagridview

Started by
2 comments, last by gan 16 years, 3 months ago
I'm looking for a way to loop through my data grid and output the rows to a text file.

TextWriter writer = new StreamWriter("Customers.txt");

for (int i = 0; i < dgvCustomers.Rows.Count; i++)
{
      /* 
      This is where I'm stuck.
      I want to now what parameters
      I need to output the current
      row
       */
      writer.WriteLine();
}

writer.Close();

Advertisement
The foreach statement is your friend.

foreach( DataGridViewRow row in dgvCustomers.Rows){    // write row here    WriteRow(row);}
Mike Popoloski | Journal | SlimDX
Thanks, but how do I get it to write out the line into a text file? I've tried a bunch of different methods, but all I get is: DataGridViewRow { Index=1 }
Quote:Original post by Kenny77
Thanks, but how do I get it to write out the line into a text file? I've tried a bunch of different methods, but all I get is: DataGridViewRow { Index=1 }

Are you trying to retrieve the value for each column for each row?
it should be something like this (I just showing you the concept; the methods/properties used might not correct, use MSDN or intellisense to find the exact methods & properties:
stringbulder temp = new string builder;foreach(datagridrow dgr in Datagridrows){    // this.dataGridView1.Rows[current row].Cells[current cell].Value    foreach(DataGridViewCell cell in dgr.Cell.Length)    {       temp.append(cell.Value);    }}StreamWriter sw=new StreamWriter("c:\\yourfile.txt", true, Encoding.ASCII);string NextLine=temp.ToString();sw.Write(NextLine);sw.Close();

This topic is closed to new replies.

Advertisement