[英]I want textbox column should be DateTime column in a datagridview in C#?
i am writing the following code in cell formatting event of datagridview in c# 我在c#中的datagridview的单元格格式化事件中编写以下代码
this.dgvfrm.Columns["expdt"].DefaultCellStyle.Format = "dd/MM/yyyy";
after writing this when i enter dd/MM/yyyy format in datagridview textbox column it shows an error. 当我在datagridview文本框列中输入dd / MM / yyyy格式时编写此文件后,它显示错误。 I show you the image 我给你看图片
i don't understand what i want to do know? 我不明白我想做什么?
之所以会出现此错误,是因为数据网格单元中的值(至少其中一个)不是有效的日期和时间,并且无法以这种方式进行格式化。
Have you tryed something like this : 您是否尝试过这样的事情:
private void dgvfrm_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex != yourFormattigColumnIndex || e.Value == null) //Added the null check
return;
dgvfrm.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ((DateTime)e.Value).ToString("dd/MM/yyyy");
}
I think this should work (might have some typo/syntax but the idea is there) 我认为这应该可行(可能会有一些错别字/语法,但是这里有这个想法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.