This example will read a csv file into a dataset and save it back when you press button 1

image_pdfimage_print

//This example code is from eran.rivlis at gmail.com

DataTable dt = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
string conString = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:csv” +
@”;Extended Properties=””Text;HDR=No;FMT=Delimited”””;
OleDbConnection conn = new OleDbConnection(conString);
OleDbDataAdapter da = new OleDbDataAdapter(@”Select * from table1.csv”, conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
StringBuilder sbCSV = new StringBuilder();
int intColCount = dt.Columns.Count;
foreach (DataRowView dr in dt.DefaultView)
{
for (int x = 0; x < intColCount; x++) { sbCSV.Append(dr[x].ToString()); if ((x + 1) != intColCount) { sbCSV.Append(","); } } sbCSV.Append(" "); } using (StreamWriter sw = new StreamWriter(@"c:csv able1.csv")) { sw.Write(sbCSV.ToString()); } } [/csharp]