Question DataGrid Export Csv

kwhelchel

Well-known member
Joined
Feb 28, 2020
Messages
53
Programming Experience
Beginner
This is what I ave datagrid first 3 columns are from a csv file. The rest of the columns will be a column combobox with a list of times in it. The combobox will reflect a day of the week with an in out time for each row.
I have the combobox displaying a drop down which works on a double click ? prefer a single click to get in it. Once every person in the list has had the times for in and out selected for the days they work I will then be doing two things with it.
1st is save the schedule as a new csv file that they can name.
2nd is I will be doing a new function to commit the schedule and that will be in an ini file.

So first thing to do is the save to new csv.
xaml file:
<Window.DataContext>
        <custom:GetCsvEmp/>
    </Window.DataContext>

    <Grid Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="EmpList" ItemsSource="{Binding EmpList}" AutoGenerateColumns="False"
                  AlternatingRowBackground="PowderBlue" Padding="120,20" Background="Gray" Margin="10,50,-10,20" Grid.RowSpan="2">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Employee"
                                    Binding="{Binding Path=EmpName}" Width="80"/>
                <DataGridTextColumn Header="Payroll ID"
                                    Binding="{Binding Path=EmpPayrollID}" Width="150"/>
                <DataGridTextColumn Header="Job"
                                    Binding="{Binding Path=JobName }" Width="110"/>
                <DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Time In"
                                        SelectedItemBinding="{Binding TimeIn}"/>
                <DataGridComboBoxColumn x:Name="ComboBoxColumn2" Header="Time Out"
                                        SelectedItemBinding="{Binding TimeOut}"/>

            </DataGrid.Columns>

        </DataGrid>

code behind it for the import csv.
Csv File import:
   class GetCsvEmp
    {

        public List<EmpCsvGet> EmpList { get; set; } = GetEmpget();
        public static List<EmpCsvGet> GetEmpget()
        {
            var file = @"c:\rcs\export\timecard.csv";
            var lines = File.ReadAllLines(file);
            var list = new List<EmpCsvGet>();
          
            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Split(',');
                var empCsvGet = new EmpCsvGet()
                {
                    EmpName = line[0],
                    EmpPayrollID = line[1],
                    JobName = line[11]
                  

                };
                list.Add(empCsvGet);
              
                        

            };
            return list;
        }
    }
 

    public class EmpCsvGet
    {

        // [Index(0)]
        public string EmpName { get; set; }
        //  [Index(1)]
        public string EmpPayrollID { get; set; }
          //[Index(11)]
        public string JobName { get; set; }
        

    }

mainwindow.xaml.cs code:
 public partial class MainWindow : Window
    {
  
    public ObservableCollection<string> TimeIn { get; set; }
        public ObservableCollection<string> TimeOut { get; set; }
        public MainWindow()
    {
      TimeIn = new ObservableCollection<string>() { "7:00a", "8:00a", "9:00a", "10:00a", "11:00a", "12:00p", "1:00p", "2:00p", "3:00p",
          "4:00p", "5:00p", "6:00p", "7:00p", "8:00p", "9:00p", "10:00p", "11:00p", "12:00a", "1:00a", "2:00a", "3:00a", "4:00a" };


      TimeOut = new ObservableCollection<string>() { "7:00a", "8:00a", "9:00a", "10:00a", "11:00a", "12:00p", "1:00p", "2:00p", "3:00p",
          "4:00p", "5:00p", "6:00p", "7:00p", "8:00p", "9:00p", "10:00p", "11:00p", "12:00a", "1:00a", "2:00a", "3:00a", "4:00a" };



            InitializeComponent();
      ComboBoxColumn.ItemsSource = TimeIn;
            ComboBoxColumn2.ItemsSource = TimeOut;

        }

        
    }

Thanks
 
As a quick aside, simply splitting by commas is not going to work when you have an employee named "Thurston Howell, III".
 
HAHAHA good point but the names for employee comes from one column in the csv file and that is an export to csv from the other system( i went back and looked at it)
 
Anyway saving back to CSV is a simple as iterating over the EmpList and writing out the values. The fun part will be determining what values were selected in the drop down columns since you aren't currently binding the values back into the EmpCsvGet instances.
 
Can I add in the time frames to the csv file when loaded and bind to that column to create the binding to the combobox
 
Thanks that was a big help on understanding and cleanup.
problem i have is how do I do the enum like this "7:00a"?
once I get past that then i need to save the datagrid back to a new csv file
 
The sample code in the documentation uses SelectedValueBinding and has an enum as the bound value and the items source as a list of enum values. My gut feel is that if you used TextBinding and have a string as the bound value and the items source as a list of string values, you'll get the effect that you want for the least effort.

This does bring up an interesting design issue. In general, you don't want to be stringly-typed. Ideally, you would want to be strongly-typed. For things to be strongly-typed, then the recommend solution is use SelectedValueBinding and have a TimeSpan as the bound value and the items source as a list of TimeSpan values. You'll likely need to play with TimeSpans anyway when you need to compute the number of hours worked. You'll just have to put in some extra effort for getting "friendly" strings to be displayed in the UI, and if you want the CSV files to also have "friendly" strings stored. By "friendly strings", I mean having the time with a AM/PM suffix. As I recall the out of the box TimeSpan formatters don't support it, but my memory could be failing me.

Hopefully others on this forum will offer their opinion on whether to stick with enums, strings, or switch over to TimeSpan. Right now I'm running on 4 hours of sleep and a cup of coffee so I'm tired and weary, and my suggestion is to stick with strings because it will be the least friction. It's only later if you discover that you repeatedly have to parse the string to create a TimeSpan so that you can calculate things, then it maybe worth crossing the bridge of updating the code to use the more accurate type.

 
Back
Top Bottom