Click here to Skip to main content
15,790,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In the program I am currently creating, I have textboxes which when filled, add a row to the DataGridView.

I am wondering on how I can get it so when I confirm the textboxes and add the row to the DataGridView, get it to write to and save to a CSV file, with automatic saving.

Along with this, I want it so when I open/run the solution, for all the tasks to be loaded into the DataGridView.

Some side Question?
- How can I create it so that when a row is selected using a checkbox function on the DataGridView, to then send this row to another DataGridView in another form.

- Should I instead write all the data to TextFiles?

What I have tried:

The code below is used to create a new entry, when the textboxes are filled and the confrim button is pressed, it writes into a new Row on the dataGridView (dgvGeneral)

 Private Sub btnAddTask_Click(sender As Object, e As EventArgs) Handles btnAddTask.Click
Dim dgvGeneralRow As Object() = New Object() {Nothing, txtTaskName.Text, txtTaskTag1.Text, cmbPriority.SelectedItem.ToString(),dateDUE.Value.ToShortDateString(), txtLink.Text, txtNoteEntry.Text}

        txtTaskName.Text = ""
        txtTaskTag1.Text = ""
        txtLink.Text = ""
        txtNoteEntry.Text = ""
Updated 16-Jul-20 2:55am

CSV can be funny stuff, particularly when using "free format" input like task names, descriptions etc - and user input that contains commas can mess it up completely unless you double quote the entries. Then you have to escape the double quotes the user typed, and so on. And than you have to parse that lot when you read it back.

Nowadays, it's probably better to use JSON - I use NewtonSoft (do a Nuget search) which can handle pretty much whatever data source you throw at it: DataTables, Collections, it doesn't really mind.
Share this answer
As OriginalGriff suggests, using something like JSON is a much better idea these days. However, if the data is being presented to you as CSV then the following article shows an easy way to get it straight into the DataGridView:
Using OleDb to Import Text Files (tab, CSV, custom)[^].

Note that the Microsoft Jet engine has been superseded by the ACE driver Download Microsoft Access Database Engine 2016 Redistributable from Official Microsoft Download Center[^].
Share this answer
Shaheer Rizwan 16-Jul-20 19:30pm    
Should I instead save the DataGridView Rows as Textfiles, which are seperate for each DataGridView I have. The main problem I am having is figuring out how I can get a selected row to be sent to another DataGridView in another form
Shaheer Rizwan 16-Jul-20 19:39pm    
I should make a point that I actually have a DataGridView desgined with headers (column headings) and don't want to change it. I simply want it so when the textboxes are filled in and confirmed and the Task is added to the DataGridView, for it to save to the CSV file (or XML or Text), and load automatically when the program is then opened. I also want it for a selected row (via the dataGridViewCheckBoxColumn) to be moved to another DataGridView in another form.
Richard MacCutchan 17-Jul-20 3:12am    
None of these things are particularly complex. Saving a DataGridView to XML requires very little code as described at How to update the XML file from datagridview[^]. Moving a row from one grid to another is similarly just a question of instantiating the second form and calling some method that accepts the row data as its parameter.
Shaheer Rizwan 18-Jul-20 3:47am    
Thanks I'll have a look

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