【问题标题】:How do I add a timestamp to a gridview如何将时间戳添加到网格视图
【发布时间】:2015-02-26 11:27:24
【问题描述】:

如何向显示上次编辑行的时间的网格视图添加时间戳?我一直在谷歌搜索,但还没有找到任何有用的东西。

我尝试创建一种方法,可以在您编辑或添加列时更新时间。但我不知道如何更进一步,比如将其解析到每一列。

我的表格列

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

【问题讨论】:

    标签: c# xml winforms datetime datagridview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        更新数据表中记录的最简单方法是使用 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]);
        }
        

        【讨论】:

        • 我不知道从哪里开始或如何应用它来满足我的需求
        猜你喜欢
        • 2016-12-16
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多