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