Sign in to follow this  

[C#] Looping Through Datagridview

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 }

Share this post


Link to post
Share on other sites
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();


Share this post


Link to post
Share on other sites

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this