【问题标题】:How do I automate a date to be inserted into a datagridview?如何自动将日期插入到 datagridview 中?
【发布时间】:2019-08-08 07:23:42
【问题描述】:

[已解决,我已在底部添加答案]

[原问题] 如何自动添加要插入数据网格视图的日期?

datagridview 没有绑定或来源到任何 SQL 服务器,因为用户将写入 DataGridView 的任何内容都将被提取并转换为 pdf 表。但是,DataGridView 中的日期将被固定为今天的日期。

我的最终结果应该如下图所示:

我尝试使用下面的代码自动将日期添加到新行中,但没有成功。

dt.Rows.Add("CurrentDate",System.Type.GetType("System.DateTime"));

注意 - 我确实添加了一个数据表。

我还将方法从 CellContentClick 更改为 CellContentDoubleClick。

编辑 1:

我更新了 DGV,因为显然我仍然需要一个数据表来分配作为源来完成这项工作。

这是我的代码:

pretable = new DataTable();
            pretable.Columns.Add("NO", typeof(int));
            pretable.Columns.Add("ID", typeof(string));
            pretable.Columns.Add("LO", typeof(string));
            pretable.Columns.Add("QT", typeof(string));
            pretable.Columns.Add("DA", typeof(DateTime));
            pretable.Columns["DA"].DefaultValue = DateTime.Now;
            pretable.Columns.Add("ER", typeof(string));
            pretable.Columns.Add("RE", typeof(string));
            DataRow row = pretable.NewRow();
            row["DA"] = DateTime.Now;
            pretable.Rows.Add(row);
            dataGridView1.DataSource = pretable;
            dataGridView1.Rows[5].Cells["DACL"].Value = DateTime.Now;

DA = DataTable 中的日期列

DACL = DGV 中日期列的名称

但是,我的结果只添加了一个空行,如下图所示。

.

.

[回答]

之前在 Form_Load 上,我添加了一个 DataTable,但是我发现这个 DataTable 不需要绑定到 DGV 作为日期工作的源。

所以我在 Form_Load 上添加了这段代码

dataGridView1.Rows[0].Cells["DACL"].Value = DateTime.Now.ToString("dd/MM/yyyy");
dataGridView1.Rows[0].Cells["NOCL"].Value = totalRowsN;

这将作为我的第一个行编号和日期插入。

感谢下面的 ZedLepplin 提供基本答案。

我发现 DataGridView1_CellContentDoubleClick 需要太多点击才能发生事件,所以我改为使用 DataGridView1_CellClick 插入现在需要更少的努力。

我使用的代码是:

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            totalRowsC = dataGridView1.Rows.Count-1;
            totalRowsN = dataGridView1.Rows.Count;
            dataGridView1.Rows[totalRowsC].Cells["DACL"].Value = DateTime.Now.ToString("dd/MM/yyyy");
            dataGridView1.Rows[totalRowsC].Cells["NOCL"].Value = totalRowsN;
        }

这样做的目的是 TotalRowsC 将是代码的 Index 值

dataGridView.Rows[Index].Cells[ColumnName]

使其适合编程索引。编程索引从 0 开始,即使我们看到的行是 1。如果我没有在 TotalRowsC 中减一,则会提示错误,说明我们需要插入一个适合现有索引的值。

日期自动化问题已解决。 谢谢大家的帮助。

【问题讨论】:

  • 数据表的用途是什么,它真的只有 1 列吗?我建议您将 dgv 中的日期列设置为只读,并在添加新行时设置内容。
  • @Filburt - 让我检查一下。
  • @TaW - 好的,我明白你说的只有 1 列是什么意思。我必须使我的数据表列具有与 DGV 相同的列数,然后只有我可以根据哪一列写入 DGV。对吗?
  • 是的,我很困惑。你不是说 DGV 不是数据绑定的吗?

标签: c# winforms datagridview


【解决方案1】:

要将日期添加到列中,您要添加日期的行必须已经存在。

所以假设该行存在并且日期列是一个文本列,那么这对我有用:-

yourDgvName.Rows[0].Cells["yourColumnName"].Value = DateTime.Today.ToShortDateString();

将 yourDgvName 替换为 DataGridView 控件的名称。

将 yourColumnName 替换为日期列的名称,或者只使用索引号而不是字符串列名。

这会将今天的日期添加到 DataGridView 控件第一行的日期列中。

或者,您可以创建一个 DataGridView 行,将今天的日期添加到该行数据,然后将该行添加到 DataGridView 控件。

【讨论】:

  • 这是我在 Form_Load 中的做法``` pretable = new DataTable(); pretable.Columns.Add("DA", typeof(DateTime)); pretable.Columns["DA"].DefaultValue = DateTime.Now; DataRow 行 = pretable.NewRow();行[“DA”] = 日期时间。现在; pretable.Rows.Add(row); dataGridView1.DataSource = pretable; dataGridView1.Rows[5].Cells["DACL"].Value = DateTime.Now; ```
  • 似乎无法在这里发表评论。让我补充一下我的问题
  • 我已经编辑了我的问题以反映这些变化。添加图像以更好地表示问题
  • 我对 CellContentDoubleClick 的引用感到困惑。您是希望在向数据表中添加数据时添加日期,还是在双击某些内容时显示数据?
  • 现在我更困惑了,我试过你的代码,它对我有用。但是我怀疑我知道为什么。当我将 DataGridView 控件添加到我的表单时,我没有编辑它,我只是添加了一个完全空的 DataGridViewControl。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
相关资源
最近更新 更多