// create one file gridview.csv in writing mode using streamwriter StreamWriter sw = new StreamWriter("gridview.csv"); // now add the gridview header in csv file suffix with "," delimeter except last one for (int i = 0; i < dataGridView1.Columns.Count; i++) { sw.Write(dataGridView1.Columns[i].HeaderText); if (i != dataGridView1.Columns.Count) { sw.Write(","); } } // add new line sw.Write(sw.NewLine); // iterate through all the rows within the gridview foreach (DataGridViewRow dr in dataGridView1.Rows) { // iterate through all colums of specific row for (int i = 0; i < dataGridView1.Columns.Count; i++) { // write particular cell to csv file sw.Write(dr.Cells[i].Value +","); if (i != dataGridView1.Columns.Count) { // sw.Write(","); } } // write new line sw.Write(sw.NewLine); } // flush from the buffers. sw.Flush(); // closes the file sw.Close();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)