Click here to Skip to main content
14,700,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
            //ds.AcceptChanges();

            String[] all_text = File.ReadAllText(@"c:\adataFile.csv").Split('\n');
            int count_row = 0;
            foreach (string row in all_text)
            {
                //MessageBox.Show(line);
                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.
Posted
Comments
ZurdoDev 8-May-14 16:36pm
   
Just like you read the data in with ReadAllText, you'll need to write it out too. Use the File object as well.
Danut D 8-May-14 17:20pm
   
can you be a little more specific, please?
ZurdoDev 8-May-14 17:36pm
   
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
Danut D 8-May-14 17:41pm
   
All the methods there that are "writing" are creating a new file. I am sorry but I still cannot figure out what to do.
ZurdoDev 8-May-14 17:44pm
   
Most only create a new file if one does not exist.
Do you understand the code you currently have? You basically just need to go the other way.

Loop through your DataGrid building up a string of values and then write it to a text file.
Danut D 8-May-14 17:49pm
   
So you are saying that I need to replace all I have in the csv file, with the data in my grid?
I will try to do that.
ZurdoDev 8-May-14 17:50pm
   
Isn't that what you are asking for? Perhaps I misunderstood.
Danut D 9-May-14 15:58pm
   
What i was looking for is some way to commit the changes back to csv the way it works for sql table. If i have an sql table as source for the datagrid, using the sqlcommandbuilder i can submit the changes back to database very easy. And as far as i know only the modified records are changed.
ZurdoDev 9-May-14 16:01pm
   
No, csv does not work that way. It's just a text file.
Danut D 10-May-14 3:52am
   
Yeah, I understood that now, thanks for taking time with me :)
syed shanu 9-May-14 0:42am
   
Do you want to load the csv file data to gridview,If so try like this :

private DataSet ds = new DataSet();
private void button4_Click(object sender, EventArgs e)
{
ds = new DataSet();
ds.Tables.Add();

String[] all_text = File.ReadAllText(@"c:\adataFile.csv").Split('\n');
int count_row = 0;
foreach (string row in all_text)
{
//MessageBox.Show(line);
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++;
}
ds.AcceptChanges();

dataGridView1.DataSource = ds.Tables[0].DefaultView;

}
//Using break point check in ds do you have all the data from CSV file.
Danut D 9-May-14 15:55pm
   
this works as well, thank you.
Danut D 10-May-14 3:55am
   
But what i was looking for is to submit the changes back to csv. I will have to rewrite all lines in the file back from dataset to csv since I cannot tell which rows are changed.
syed shanu 11-May-14 20:21pm
   
Chk this links for Dataset to CSV :
http://www.codeproject.com/Tips/665519/Writing-a-DataTable-to-a-CSV-file
http://stackoverflow.com/questions/4959722/c-sharp-datatable-to-csv
http://stackoverflow.com/questions/888181/convert-datatable-to-csv-stream

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900