Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid to CSV

Status
Not open for further replies.

Gantonmaz

Programmer
Joined
Mar 8, 2011
Messages
2
Location
GB
HI i'm trying to get a datagrid to export to a csv. Code is below but all I get is a blank CSV. Can anyone please point me in the right direction??

//seperate datagrid to comma seperated values
string strRowVal = "";
foreach (DataGridItem item in layoutsGrid.Items)
{
string str = "";
for (int i = 0; i < item.Cells.Count; i++)
{
str += item.Cells.Text + ",";
}
str = str.Substring(0, str.Length - 1);
}


//append to csv file
StreamWriter sw = File.AppendText("e:\\results\\decdiy.csv");
{

sw.WriteLine(strRowVal);
}
sw.Close();
 
Looping through the grid itself is going to be laborious. I would loop through the datasource for the datagrid. It will be much eaiser, straight forward and maintainable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top