Question

I have datagridview which reads some strings and some dates from file. Here is dictionary which i use to store this data (used for adding new records and store the datetime values)

private Dictionary<string, DateTime> gDict = new Dictionary<string, DateTime>();

Thats how i save data to file:

 public bool SaveTableToFile()
        {
            System.Data.DataTable curr_table = dgv.DataSource as System.Data.DataTable;
            try
            {
                using (System.IO.StreamWriter strwr = new System.IO.StreamWriter(filePath + @"\" + fileName, false))
                {
                    strwr.WriteLine("__FILE_BEGIN__");
                    foreach (KeyValuePair<string, DateTime> kvp in gDict)
                    {
                        strwr.WriteLine("__LINE_BEGIN__");
                        strwr.WriteLine("Date=" + kvp.Value.ToString("dd.MM.yyyy hh:mm:ss"));
                        strwr.WriteLine("IP=" + kvp.Key);
                        strwr.WriteLine("__EOL__");
                        strwr.Flush();
                    }
                    strwr.WriteLine("__EOF__");
                    strwr.Close();
                    return true;
                }
            }
            catch { return false; }
        }

//just noticed that im missing finally block here and my catch is terrible

And thats how i read it from file http://pastebin.com/uZN4CdxZ

This dates displaying in table as timespan -

DateTime now = DateTime.Now;
...
 DateTime dt = new DateTime();//15.03.2013 3:01:13 //dd.MM.yyyy hh:mm:ss
 dt = DateTime.ParseExact(preparedString[1], "dd.MM.yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
 TimeSpan timediff = (now - dt);

And when i run my program, it reads all entries from file, calculating time difference and displays it in table. And there goes problem - all time difference is wrong, it has extra 12 hours. example of table

When i add new row after table is loaded

  public void TryAddNewRow(string ex_IP)
        {
            if (!gDict.ContainsKey(ex_IP))
            {
                System.Data.DataTable curr_table = dgv.DataSource as System.Data.DataTable;
                System.Data.DataRow theRow = curr_table.NewRow();
                DateTime dt = DateTime.Now;
                theRow["Date"] = (dt - DateTime.Now);
                theRow["IP"] = ex_IP;
                curr_table.Rows.Add(theRow);
                gDict.Add(ex_IP, dt);
            }
            else
            {
                TimeSpan ts = DateTime.Now - gDict[ex_IP];
                System.Windows.Forms.MessageBox.Show("IP already exists, it was used" + Environment.NewLine + ts.Hours+" hours, "+ts.Minutes +" minutes, and "+ts.Seconds+" seconds ago.");
            }
        }

First it displays zeros (something like that 0:00:00). But when table refreshes by timer which runs every minute, it becomes like 12 hours and one minute.

   private void timer1_Tick(object sender, EventArgs e)
        {
            gTable.SaveTableToFile();
            gTable.UpdateTable();
            label1.Text = DateTime.Now.ToString("HH:mm.ff") + ": Table updated.";
        }

 public void UpdateTable()
        {
            CreateNewDataSource();
            //reading data from file and creating rows            
            ReadTableFromFile();
        }

        private void CreateNewDataSource()
        {
            dgv.DataSource = null;
            System.Data.DataSet myDataSet = new System.Data.DataSet();
            System.Data.DataTable aTable = new System.Data.DataTable("Table 1");
            myDataSet.Tables.Add("Table 1");
            List<System.Data.DataColumn> columnsList = new List<System.Data.DataColumn>();
            foreach (TableField curr_field in fTableFields.ftfList)
                columnsList.Add(new System.Data.DataColumn(curr_field.Name, curr_field.FieldType));
            foreach (System.Data.DataColumn column in columnsList)
                myDataSet.Tables["Table 1"].Columns.Add(column);
            dgv.DataSource = myDataSet.Tables["Table 1"];
            foreach (TableField curr_field in fTableFields.ftfList)
                dgv.Columns[curr_field.Name].Width = curr_field.HeaderWidth;
        }
Était-ce utile?

La solution

The issue stems from an inconsistent parsing format.

In some places you are using hh (12-hour format), while in other places you are using HH (24-hour format).
In other words, you are parsing the time as if it is a 12-hour format, but you are displaying it as if it's a 24-hour format.

So "1:30" parses as 13:30, and is displayed as "13:30", even though it may have originaly been "01:30" (AM).

Replace all your formats to use HH.

See Custom Date and Time Format Strings for details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top