Click here to Skip to main content
14,933,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I have a program that records the times, the code writes the start time in the Start text field and the end time in the End text field, this would be saved as a list in the Observable Collection.
then the list is added to the datagrid table then the whole is written to a text file.

Now I could transform the format in text fields as desired and I could also transform everything in text files by overwriting the To string method in Timer class and returning it in the desired format as a string.

the problem is how can I transform the formats in datagrid?

the difference happens here:

I don't know if ObservableCollection would be a good choice and if you can get to every single element in this kind of list and change the format?

instead of:
00: 00: 00.000000
these will:
00:00:00


What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Drawing;
using System.Linq;
using System.IO;

namespace TimeRecorderPro
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public DateTime startTime;
        public DateTime endTime;
        public Timer _currentTimer;
        public DateTime Dauer;

        System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();

        public void SetinTab()
        {
            Timers.Add(_currentTimer);
            btnStart.Content = "Start";
        }
        void Löschen()
        {
            txtStart.Text = "";
            txtEnde.Text = "";
        }
        public void BemerkungEinfuegen(string str)
        {
            _currentTimer.Bemerkung = str;
        }
        private ObservableCollection<Timer> Timers;
        public MainWindow()
        {
            InitializeComponent();
            Timers = new ObservableCollection<Timer>{};
            lstNames.ItemsSource = Timers;
            this.ShowInTaskbar = false;
            
            notifyIcon.Icon = new System.Drawing.Icon(@"C:\Users\mmohammadi\source\repos\WPF Proj\TimeRecorderPro\TimeRecorderPro\bin\Debug\66164.ico");
            
            notifyIcon.MouseClick += notifyIcon_MouseDoubleClick;
        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if ((string)btnStart.Content == "Start")
            {
                _currentTimer = new Timer();
                txtStart.Text = _currentTimer.Start.ToString("T");
                btnStart.Content = "Ende";
            }
            else
            {
                _currentTimer.Ende = DateTime.Now;
                txtEnde.Text = _currentTimer.Ende.ToString("T");

                _currentTimer.Dauer = _currentTimer.Ende - _currentTimer.Start;

                txtEnde.Text = _currentTimer.Ende.ToString("T");

                Bemerkung bemerkungsdialog = new Bemerkung();
                bemerkungsdialog.Owner = this;
                bemerkungsdialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                bemerkungsdialog.ShowDialog();

                SetinTab();
                Löschen();
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult mbresult = System.Windows.MessageBox.Show("Möchten Das Programm wirklich beenden?", "Programm beenden", MessageBoxButton.YesNo);

            if (MessageBoxResult.No == mbresult)
            {
                e.Cancel = false;

            }

            e.Cancel = true;
            this.Visibility = Visibility.Hidden;

            string testtext = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Zeitbuchung_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";

            var objWriter = new System.IO.StreamWriter(testtext);

            foreach (Timer timer in Timers)
            {
                objWriter.WriteLine(timer.ToString());
            }

            objWriter.Close();
            notifyIcon.Visible = true;
        }

        private void lstNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
            }
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            notifyIcon.Visible = false;
            this.Show();
        }
    } 

    public class Timer
    {
        public Timer()
        {
            Start = DateTime.Now;
        }

        public DateTime Start { get; set; }
        public DateTime Ende { get; set; }
        public TimeSpan Dauer { get; set; }
        public string Bemerkung { get; set; }

        public override string ToString()
        {
            return "Start : " + Start.ToString("T") + " \nEnde  : " + Ende.ToString("T")
                   + "\nDauer : " + Dauer.TotalHours.ToString("0.00") + "\nBemerkung : " + Bemerkung.ToString();
        }
    }    
}
Posted
Updated 2-Dec-20 2:21am
v2

1 solution

For formatting a DataGridView Column that displays DateTime values, you set the DefaultCellStyle.Format property of the Column: see [^] for examples.

Study the standard format specifiers for DateTime and TimeSpan here: [^]

... and the Custom specifiers here: [^]

You have still not answered the question about what you are using ObservableCollection for ... your code does not implement any handlers for its events.

Did you get serialize/desterilize working ?
   
Comments
MMazi 2-Dec-20 9:40am
   
Thanks for the answer, I haven't worked with serialize / desterilize yet, this version should be kept simple in the next step but it interacts with other programs and I have to work with serialize / desterilize and Json.
BillWoodruff 2-Dec-20 11:22am
   
you're welcome. keep in mind that an ObservableCollection only raises events for modification of the collection items ... adds, deletes, moves, replace, reset. it will not fire any event when an internal field or property of its items has its VALUE changed. for that, you need to use INotifyPropertyChange.

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