Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi developers,

I am vishal sharma
I'm loading a csv file into datagridview using dataset and perform some operation on them. After all that I want to save the datagridview in csv file format and if user wants to the excel format.

I've taken a dialogbox to save data in csv file or excel file format.
A problem is that I have no code to save datagridview in csv file or excel file format.

I also want that a previous csv file name automatically show in the save dialogbox if the user wants to save data with same file name or a orther name, at any location.

Please provide a solution immediatly
Posted
Updated 11-Nov-10 20:46pm
v2

C#
private void button1_Click(object sender, EventArgs e)
{
string strValue = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
{
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
{
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
}
}
}
// strValue = strValue + Environment.NewLine;
}
string strFile = @"C:\YourCSVFile.csv";
if (File.Exists(strFile)&& !string.IsNullOrEmpty(strValue))
{
File.WriteAllText(strFile, strValue);
}
}
}


Or you can have THIS[^] domestic article to be further useful.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Hi,

Please you can download from following link:

Export GridView to Excel or CSV[^]


Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be good link.:rose:

Thanks,
Imdadhusen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900