将DataGridView单元格值转换为DateTime [英] Convert a DataGridView Cell Value into a DateTime

查看:387
本文介绍了将DataGridView单元格值转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个答案一段时间。

I've been searching for some time now for this answer:

我有一个datagridview,其中有一个带有日期的单元格,我需要将该单元格中的值可以与我的DateTimePicker一起使用的DateTime值

I've got a datagridview that has a cell with dates, and I need to make the value in that cell a DateTime value that can go with my DateTimePicker

到目前为止,我已经完成了此操作,但是它不起作用,它给了我System.FormatException(显然它不能将字符串识别为DateTime,听起来正确)

So far I've done this, but it's not working, it gives me a System.FormatException (apparently it can't recognize the string as a DateTime, which sounds about right)

这是有问题的代码:

int index = ReservationDataGridView.SelectedRows[0].Index;
string date = ReservationDataGridView[0, 1].Value.ToString();
DateTime test = DateTime.ParseExact(date, "dd/MM/yyyy - hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);

MessageBox.Show(test.ToString()

这就是我的方式DateTimePicker的格式为:

This is how my DateTimePicker is formatted:

ReservationDateTimePicker.Format = DateTimePickerFormat.Custom;
ReservationDateTimePicker.CustomFormat = "dd/MM/yyyy - hh:mm tt";

这就是日期在DataGridView中的样子:

And this is how the dates look in the DataGridView:

关于如何将DataGridView的Date值转换为适合该格式的DateTimePicker的DateTime的任何想法?

Any ideas on how I can convert the DataGridView's Date value into a DateTime that will fit into a DateTimePicker with that formaT?

推荐答案

而不是从DataGridview单元格中获取 DateTime 值的字符串表示,而是检索实际的<$来自DataDridview数据源的c $ c> DateTime 值。

Rather than take the string representation of the DateTime value from the DataGridview cell, instead retrieve the actual DateTime value from the DataDridview's datasource.

这篇关于将DataGridView单元格值转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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