try { string outputFile = ConfigurationManager.AppSettings["csvFileName"].ToString(); outputFile = CSVFilePATH + "\\" + outputFile ; if (dataView.RowCount > 0) { string value = ""; DataGridViewRow dr = new DataGridViewRow(); StreamWriter swOut = new StreamWriter(outputFile); //write header rows to csv for (int i = 0; i <= dataView.Columns.Count - 1; i++) { if (!names.Contains(dataView.Columns[i].Name)) { if (i > 0) { swOut.Write(","); } swOut.Write(dataView.Columns[i].HeaderText); } } swOut.WriteLine(); //write DataGridView rows to csv for (int j = 0; j <= dataView.Rows.Count - 1; j++) { if (j > 0) { swOut.WriteLine(); } dr = dataView.Rows[j]; for (int i = 0; i <= dataView.Columns.Count - 1; i++) { if (!names.Contains(dataView.Columns[i].Name)) { if (i > 0) { swOut.Write(","); } try { value = dr.Cells[i].Value.ToString(); } catch { value = ""; } //replace comma's with spaces value = value.Replace(',', ' '); //replace embedded newlines with spaces value = value.Replace(Environment.NewLine, " "); swOut.Write(value); } } lblMessage.Text = "Downloaded file " + outputFile + " Successfully..."; } swOut.Close(); // dataView.Visible = false; } } catch (Exception exp) { Logger.Info("Exception during File Export..\r\n" + exp.Message.ToString()); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)