Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
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}

        dataGVGeneral.Rows.Add(dgvGeneralRow)
        txtTaskName.Text = ""
        txtTaskTag1.Text = ""
        txtLink.Text = ""
        txtNoteEntry.Text = ""
Posted
Updated 16-Jul-20 1:55am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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[^].
   
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100