Click here to Skip to main content
15,889,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I add a timestamp to a gridview that shows the last time a row was edited? I've been searching Google but haven't found anything helpful yet.

I tried to create a method which will update the time when you edit or add a column. but i dont know how to take it further like parsing it to each and every column.

My table columns


C#
private DataTable GetDataTableFromDataGridview(DataGridView _grid)
{
  {
        var _oDataTable = new DataTable();
        object[] cellValues = new object[_grid.Columns.Count];
         //clearTable();
        _oDataTable.Columns.Add("Name", typeof(string));
        _oDataTable.Columns.Add("Value", typeof(string));
        _oDataTable.Columns.Add("Font", typeof(string));
        _oDataTable.Columns.Add("DateStamp", typeof(string));
        _oDataTable.Columns.Add("Comment", typeof(string));
        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;
    }

}

My method


C#
private DateTime _dateTime;
string _DateTimeFormat = "yyyy/dd/MM HH:mm:ss";
public void UpdateTheCurrentTime()
{
   _dateTime = DateTime.Now;
   _dateTime.ToString(_DateTimeFormat, CultureInfo.InvariantCulture);
}



[Edit member="Tadit"]
Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]
Posted
v2
Comments
FinickyCoder 27-Feb-15 1:24am    
are u using command field in gridview for updating d records?
Bacanzela 27-Feb-15 3:53am    
No command as i am wokring on xml file without database

1 solution

You should do this inside the Editing Events of DataGridView.

After all the codes inside the Editing Event, just update the Column's value to the current DateTimeStamp.
 
Share this answer
 
Comments
Bacanzela 27-Feb-15 3:52am    
Can you please give show me a way as am stack since yesterday?
All Events are listed - Here.

I think you need DataGridView.CellValueChanged Event. Else select any other event from that list and handle that.

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