简体   繁体   中英

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#

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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