如何向DataGridView添加时间戳 [英] How do I add a timestamp to a DataGridView
本文介绍了如何向DataGridView添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何向gridview添加时间戳,以显示上次编辑行的时间?我一直在搜索谷歌,但还没有找到任何有用的东西。
我试图创建一个方法来更新编辑或添加列的时间。但我不知道怎么进一步把它解析成每一列。
我的表格列
private DataTable GetDataTableFromDataGridview(DataGridView _grid)
{
{
var _oDataTable = new DataTable();
object [] cellValues = new object [_grid.Columns.Count];
// clearTable();
_oDataTable.Columns.Add( 名称, typeof (串跨度>));
_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;
}
}
我的方法
private DateTime _dateTime;
string _DateTimeFormat = yyyy / dd / MM HH:mm:ss;
public void UpdateTheCurrentTime()
{
_dateTime = DateTime。现在;
_dateTime.ToString(_DateTimeFormat,CultureInfo.InvariantCulture);
}
更正了格式和/或语法问题。
已添加 pre
标签。
[/编辑]
解决方案
< blockquote>您应该在 DataGridView
的编辑事件中执行此操作。
完成编辑事件中的所有代码后,只需将Column的值更新为当前的 DateTimeStamp
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
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
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]
解决方案
You should do this inside the Editing Events ofDataGridView
.
After all the codes inside the Editing Event, just update the Column's value to the currentDateTimeStamp
.
这篇关于如何向DataGridView添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文