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.
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;
}