如何向gridview添加时间戳

时间:2015-02-26 11:27:25

标签: c# xml winforms datetime datagridview

如何向gridview添加时间戳,以显示上次编辑行的时间?我一直在搜索谷歌,但还没有发现任何有用的东西。

我尝试创建一个方法,用于更新编辑或添加列的时间。但我不知道如何进一步把它解析为每一列。

我的表格列

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

    }

我的方法

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

3 个答案:

答案 0 :(得分:0)

我认为你可以在数据库方面处理这个问题。由于它是数据绑定网格视图,因此您只需要一个默认值为NOW()的日期时间字段用于MySql。对于SQL Server,我相信使用datetime.now()的持久计算列可以。

答案 1 :(得分:0)

DataGridView.CellValueChanged事件:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged%28v=vs.110%29.aspx

private void MyDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
{
    DataGridViewRow changedRow = myDataGridView.Rows[e.RowIndex];
}

答案 2 :(得分:0)

更新数据表记录的最简单方法是使用BeginEdit

private void DemonstrateRowBeginEdit()
{
DataTable table = new DataTable("table1");
DataColumn column = new 
    DataColumn("col1",Type.GetType("System.Int32"));
table.RowChanged+=new 
    DataRowChangeEventHandler(Row_Changed);
table.Columns.Add(column);

// Add a UniqueConstraint to the table.
table.Constraints.Add(new UniqueConstraint(column));

// Add five rows.
DataRow newRow;

for(int i = 0;i<5; i++)
{
    // RowChanged event will occur for every addition.
    newRow= table.NewRow();
    newRow[0]= i;
    table.Rows.Add(newRow);
}
// AcceptChanges.
table.AcceptChanges();

// Invoke BeginEdit on each.
Console.WriteLine(
    "\n Begin Edit and print original and proposed values \n");
foreach(DataRow row in table.Rows)
{

    row.BeginEdit();
    row[0]=(int) row[0]+10;
    Console.Write("\table Original \table" + 
        row[0, DataRowVersion.Original]);
    Console.Write("\table Proposed \table" + 
        row[0,DataRowVersion.Proposed] + "\n");
}
Console.WriteLine("\n");
// Accept changes
table.AcceptChanges();
// Change two rows to identical values after invoking BeginEdit.
table.Rows[0].BeginEdit();
table.Rows[1].BeginEdit();
table.Rows[0][0]= 100;
table.Rows[1][0]=100;
try
{
    /* Now invoke EndEdit. This will cause the UniqueConstraint
       to be enforced.*/
    table.Rows[0].EndEdit();
    table.Rows[1].EndEdit();
}
catch(Exception e)
{
    // Process exception and return.
    Console.WriteLine("Exception of type {0} occurred.", 
        e.GetType());
}
}

private void Row_Changed(object sender, 
   System.Data.DataRowChangeEventArgs e)
{
 DataTable table = (DataTable)  sender;
 Console.WriteLine("RowChanged " + e.Action.ToString() 
     + "\table" + e.Row.ItemArray[0]);
}