简体   繁体   English

Datagridview日期到标签格式问题

[英]Datagridview date to label formatting issue

I am almost embarrassed to ask this. 我几乎不好意思问这个。 I would like to take a date from my DataGridView named salesOrdersDataGridView and write it to a label on a Form . 我想从我的DataGridView获取一个名为salesOrdersDataGridView的日期,并将其写入Form的标签上。 I format the date to short date in my sql query and it displays correctly in the DataGridView . 我在SQL查询中将日期格式设置为短日期,并且在DataGridView正确显示了日期。 Trouble is, when I use this code: 麻烦的是,当我使用此代码时:

string shp= "";
shp = salesOrdersDataGridView.Rows[e.RowIndex].Cells["shipDate"].Value.ToString();
lblshp.Text = shp;

The label displays the long date/time (with hours and minutes). 标签显示长日期/时间(带有小时和分钟)。 I know there is a simple solution - but I am just not getting it. 我知道有一个简单的解决方案-但是我不明白。 Thanks in advance! 提前致谢!

DateTime shp = DateTime.Parse(salesOrdersDataGridView.Rows[e.RowIndex].Cells["shipDate"].Value.ToString());

lblshp.Text = shp.ToString("dd MMM yyyy");

Parse your value as a datetime first and use the toString method to format it 首先将您的值解析为日期时间,然后使用toString方法设置其格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM