Experts,
I'm trying to determine how to export the contents (e.g. row/cells) of an unbound DataGrid to a text file via button_click event. The datagrid contains a fixed number of columns (16). Ultimately, I'm trying to have user click a button resulting in the exporting of rows to a .txt file (e.g. c:\test.txt). So, if the datagrid has the following information:
Car Engine Doors Color Vin
Ford V6 4 blue 454545454
Dodge v4 2 green 565686533
Honda i4 2 red 4545454545
It would create the follow test.txt:
Ford V6 4 blue 454545454
Dodge v4 2 green 565686533
honda i4 2 red 4545454545
Unfortunately, I haven't gotten very far. I'm able to read the contents of the cells into a string, but I can't determine how to write it to a text file.
{
StringBuilder sb = new StringBuilder();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
foreach (DataGridViewCell cell in row.Cells)
{
sb.Append(" ");
sb.Append(cell.Value.ToString());
}
// using (StreamWriter sw1 = new StreamWriter(@"C:\test.txt"))
MessageBox.Show(sb.ToString());
{
}
}
}
}
}
}
Loading

theLizardRank #1,079 · 1.8K reputationPosted Mar 3, 2010, 8:30 AM EST
I have sent you some code in a private message to you it will do what you need.
I have not posted it here because here are some on this forum who use other peoples code in articles and claim it as their own.
matthew king51 reputationPosted Mar 3, 2010, 8:45 AM EST
Again, thanks for the help you have provided over the last few months!!
theLizardRank #1,079 · 1.8K reputationPosted Mar 3, 2010, 9:05 AM EST
Good stuff.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 3, 2010, 2:30 PM EST