Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[C#] Looping Through Datagridview


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Kenny77   Members   -  Reputation: 164

Like
0Likes
Like

Posted 08 January 2008 - 02:21 PM

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();



Sponsor:

#2 Mike.Popoloski   Crossbones+   -  Reputation: 2923

Like
0Likes
Like

Posted 08 January 2008 - 02:27 PM

The foreach statement is your friend.


foreach( DataGridViewRow row in dgvCustomers.Rows)
{
// write row here
WriteRow(row);
}



#3 Kenny77   Members   -  Reputation: 164

Like
0Likes
Like

Posted 08 January 2008 - 03:04 PM

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 }

#4 gan   Members   -  Reputation: 166

Like
0Likes
Like

Posted 12 January 2008 - 02:26 AM

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();







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS