如何更新datagridview的日期列 [英] How to update date column of a datagridview

查看:82
本文介绍了如何更新datagridview的日期列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们......



我的问题是,我无法在datagridview中编辑日期栏



我使用Access作为DB,其中有数据类型为DATETIME的字段,命名为DOB

日期时间格式为mm / dd / yyyy。

在前端我显示日期列为

Hi Friends...

My problem is, I am not able to edit date column in datagridview

I am using access as DB in which there is field with datatype DATETIME named as DOB
datetime format is mm/dd/yyyy.
In front end i am displaying the date column as

dataGridView1.Columns["DOB"].DefaultCellStyle.Format = "dd/MM/yyyy";



当我尝试编辑日期栏时抛出错误。

我如何克服这种情况。

我正在使用数据集和dataadapater从DB中获取值。

用于保存我正在使用 adapter.Update 方法



错误详细信息

Datagridview默认错误对话框

System.FormatExecption:字符串无法识别为有效的有效日期时间。

。 .....

.....

要替换此默认对话框,请处理DataError事件



更新问题


从数据库获取数据的代码


When i try to edit date column its throwing an error.
How can i overcome this situation.
I am using dataset and dataadapater to fetch the values from DB.
For saving i am using adapter.Update method

Error Details
Datagridview Default Error Dialog
System.FormatExecption: String was not recognized ass valid DateTime.
......
.....
To Replace this default dialog please handle the DataError event

Updated Question

Code for fetching data from DB

dataGridView1.DataSource = dc.dsreturn_family("Select ID,FullName,Mobile,DOB,Gender,Status from Family where Hno=" + hnumber).Tables[0];
                      dataGridView1.AutoResizeColumns();
                      dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                      dataGridView1.DefaultCellStyle.Font = new Font("Maiandra GD", 12);
                      dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
                      dataGridView1.Columns[0].Visible = false;
                      dataGridView1.Columns["DOB"].ValueType = typeof(DateTime);
                      dataGridView1.Columns["DOB"].DefaultCellStyle.Format = "dd/MM/yyyy";





错误来了。

当我尝试编辑 DOB column。

推荐答案

试试这个

try this
DateTime dt=DateTime.ParseExact(stringdate, "MM/dd/yyyy", CultureInfo.InvariantCulture)



现在将此dt传递给update语句。


Now Pass this dt in update statement .


检查列ValueType属性。如果你想以编程方式进行,你可以这样做:



Check the column ValueType property. If you want to do it programatically, you can do something like this:

dataGridView1.Columns["DOB"].ValueType = typeof(DateTime);





编辑



我现在认为这是一个问题文化。尝试在设置DefaultCellStyle之前添加此项,如果在您的文化中dd / MM / yyyy是默认日期格式:





Edit

I now think it's a problem of culture. Try adding this before setting DefaultCellStyle, if in your culture dd/MM/yyyy is the default date format:

dataGridView1.Columns["DOB"].DefaultCellStyle.FormatProvider = CultureInfo.CurrentCulture;





如果您的文化不是习惯性日期格式,请尝试以下示例:





If in your culture is not the habitual date format,try this for example:

dataGridView1.Columns["DOB"].DefaultCellStyle.FormatProvider = CultureInfo.CreateSpecificCulture("es-ES");






Hi,

using System.Globalization;//namespace
DateTime From = DateTime.ParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);





参考: CultureInfo.InvariantCulture是什么意思


这篇关于如何更新datagridview的日期列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆