Resolved CSVHelper Write Function

kwhelchel

Well-known member
Joined
Feb 28, 2020
Messages
53
Programming Experience
Beginner
I have been trying to figure out how to get csvhelper to write the data that is in my datagridview. The datgridview is populated by the following.
First 3 are populated from a csv file with a class that has the csvhelper attributes to grab certiam columns from the csv file.
Then the rest of the datagridview is done like so.
datagridview columns:
                DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
                dgvCmb.HeaderText = "Mon OUT";
                dgvCmb.Items.Add("");
                dgvCmb.Items.Add("5:00a");

And with me using what I have don I only get what is listed in the "new WriteCsvschedule { EmpName = "1", EmpPayrollID = "2" }," line only but I need all the data that is in the datagridview to be saved to a new csv file.
Any help on what I did wrong and to fix this is very much appreciated.

Datagrid save code:
{
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"c:\rcs\schedule\";     
            saveFileDialog1.Title = "Save Schedule";
                
            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "CSV files (*.csv)|*.csv|csv Files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = saveFileDialog1.FileName;
            }


            var records = new List<WriteCsvschedule>

            {
                new WriteCsvschedule { EmpName = "1", EmpPayrollID = "2" },
            
            };

            using (var writer = new StreamWriter(saveFileDialog1.FileName))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.WriteRecords(records);

            }

            EmpDataGrid.DataSource = records;


        }
 
In the code you have posted, you create a list, add one item to it, write that list to a file, then bind the list to the grid. If you expect the user to be able to add items to the list and have them written to the file then you obviously need to change the order of those steps AND wait until the user has actually added items to the list via the grid before writing the data to the file.
 
yes the user can change the values as this is a way to create a schedule limit setup. the first 3 columns would be nice if fixed. But I am still stuck lol
 
@kwhelchel did you make the suggested changes above?

Frankly if you were using WPF and not the dreaded dated Winforms, you'd not need to worry about handling data since all your data is bound in WPF to their respective classes and models/model views.

In saying that, there isn't much stopping you from taking the same approach here either... Utilizing a class object, populated from a CSV file, you are half way there.

I think you need to stop, and explain what you are trying to do, and more importantly why you are doing it the way you are?

How did your data get into a CSV file in the first place?

The CSV file which you populate your class object with, is this the same CSV file you want to add more data too from your DGV?
 
Back
Top Bottom