How to add datestamp to the datagridview



I have an xml file which the user will load to the datagridview and write to the file. all this it working fine. on the xml i have datestamp column which will load the date outomatically and it must also update the time when the user edited from other cell. i tried many examples yesterday but i didnt come with a working solution or from the question i asked. today i created a class to try to update my column but it is not working


when i debug from CellEndEdit and my class throws an error "Object reference not set to an instance of an object." , i cant even step into my class and see where the error it is.


Question : how can i get the time get updated automatically when you edit or write to the file?


my codes


my column for datestamp



private DataTable GetDataTableFromDataGridview(DataGridView _grid)
{
{
var _oDataTable = new DataTable();
object[] cellValues = new object[_grid.Columns.Count];
_oDataTable.Columns.Add("DateStamp", typeof(DateTime));
foreach (DataGridViewRow row in _grid.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
_oDataTable.Rows.Add(cellValues.ToArray());
}
return _oDataTable;
}
}

private void Gridview_Output_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
_UpdateTime.UpdateTimeNow();
}


}


my class


class UpdateTime { public UpdateTime() { }



private Regex _dateRegex = new Regex(@"(?<=\[DateStamp\]).*?(?=\[/DateStamp\])");
public string _DateFormat = "yyyy/dd/MM HH:mm:ss";
private DateTime _date;
private XElement _datenodes;

public string DateStamp
{
get
{
return _date.ToString(_DateFormat);
}
}


my class



namespace Project
{
class UpdateTime
{
public UpdateTime()
{
}

private Regex _dateRegex = new Regex(@"(?<=\[DateStamp\]).*?(?=\[/DateStamp\])");
public string _DateFormat = "yyyy/dd/MM HH:mm:ss";
private DateTime _date;
private XElement _datenodes;

public string DateStamp
{
get
{
return _date.ToString(_DateFormat);
}
}

public void UpdateTimeNow()
{
_date = DateTime.Now;
DateTime _dt;
this._datenodes.Value = string.Format("[DateStamp]{3}[/DateStamp]",this._date.ToString(_DateFormat, CultureInfo.InvariantCulture));
if (DateTime.TryParseExact(_dateRegex.Match(this._datenodes.Value).Value, _DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out _dt))
{
this._date = _dt;
}
}

}


}


No comments:

Post a Comment