I have a simple windows form with a datagridview and one csv file with 4 rows, the first being the headers.
The csv file looks like this:
Name,Age,Phone,Street
Dan,23,888765,Street1
Daisy,40,556432,Street2
Jake,12,876324155,Street3
I have read the code to populate the DataGridView1 from csv, and my code looks like this:
private DataSet ds = new DataSet();
private void button4_Click(object sender, EventArgs e)
{
ds = new DataSet();
ds.Tables.Add();
dataGridView1.DataSource = ds.Tables[0].DefaultView;
String[] all_text = File.ReadAllText(@"c:\adataFile.csv").Split('\n');
int count_row = 0;
foreach (string row in all_text)
{
string[] data = row.Split(',');
if (count_row == 0)
{
foreach (string header in data)
{
ds.Tables[0].Columns.Add(header);
}
}
else
{
ds.Tables[0].Rows.Add(data);
}
count_row++;
}
}
I have tried to add another button and add ds.AcceptChanges(); but this does not work.
I know that when getting the data from SQL table I use sqlcommandbuilder, but i cannt seem to find any builder for CSV.
Please tell me how to commit the changes made in the DataGrid back to the csv file.
Thank you.