如何使dataGridViewTextBoxColumn接收日期时间值 [英] How to make dataGridViewTextBoxColumn receive a datetime value
问题描述
使用datagridview将数据添加到表中。因此,我希望每当有人在数据表中添加新记录时,单元格将收到日期时间值。所以我这样做了:
DataGridView.CurrentRow.Cells [dataGridViewTextBoxColumn] .Value = DateTime.Now;
问题是这给出了一个错误,我不知道在哪里改变。任何人都可以给我一个提示吗?
using a datagridview to add data to a table. So i want to make everytime someone adds a new record in the datatable, a cell will receive a datetime value. So i did this:
DataGridView.CurrentRow.Cells[dataGridViewTextBoxColumn].Value = DateTime.Now;
the problem is that this gives an error and i don''t know where to change. Can anyone give me a hint?
推荐答案
嗨
我测试你的代码执行没有错误。
请关注以下点:
1)此代码没问题。
Hi
I test you code it execute without error.
Please attend to bellow point:
1)This code is OK.
dataGridView1.CurrentRow.Cells[YourColumnNumber].Value = DateTime.Now;
2)第一列编号为零,因此如果要将日期添加到第三列,则必须使用2 az YourColumnNumber
3)你可以使用Button_Click中的高位代码
我希望它是' help
2)The first column number is zero, therefore if you want add date to third column you must use from 2 az YourColumnNumber
3)You can use from upper code in Button_Click
I hope it''s helpful
编译器错误的原因是您使用了dataGridViewColumn对象来访问接受Column索引或名称的单元格。更改你的代码如
The reason for the compiler error is you have used the dataGridViewColumn object to access the cell which accepts the Column index or name. Change your code like
DataGridView.CurrentRow.Cells[columnIndex].Value = DateTime.Now; or
DataGridView.CurrentRow.Cells[columnName].Value = DateTime.Now;
其中columnIndex / columnName是表示列的数字/名称。
where columnIndex/columnName is the number/Name that represents your column.
只想感谢两者。我正在使用列名。但在阅读了你的帮助之后,我意识到使用索引会更简单。但错误仍然存在。离开代码一段时间,回来后我看到我错了。这是错误的索引。 datetime值应该转到[4],我给索引[5]这是一个字符串。错误信息非常清楚,但有时大脑会看到除此之外的一切。非常感谢你们。
Just want to thank you Both. I was using the column name. But after reading your help, i realized it''s simpler to use the index. But the error persisted. Left the code for a while, when came back i saw where i was wrong. It was wrong index. The datetime valu should go to [4] and i was giving to index [5] that is a string. The error message was pretty clear, but sometimes the brain sees everything but that. Thank you alot guys.
这篇关于如何使dataGridViewTextBoxColumn接收日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!