C# Can';t更改DataGridViewCell类型

C# Can';t更改DataGridViewCell类型,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图转换一个单元格(大多数单元格都填充了信息),但当我从数据库中获取此列时,它将为NULL。 在datagridview中,它显示为无。此单元格的类型为DateTime,但我要做的是: 如果该单元格为NULL我想将其更改为N/A,但当我将这两行代码放入时: userDataGridView.Rows[i].Cells[column] = new DataGridViewTextBoxCell(); userDataGridView.Rows[i].Cells[column].Value = "

我试图转换一个单元格(大多数单元格都填充了信息),但当我从数据库中获取此列时,它将为NULL。 在
datagridview
中,它显示为无。此单元格的类型为
DateTime
,但我要做的是: 如果该单元格为
NULL
我想将其更改为
N/A
,但当我将这两行代码放入时:

userDataGridView.Rows[i].Cells[column] = new DataGridViewTextBoxCell();
userDataGridView.Rows[i].Cells[column].Value = "N/A";
运行时,我收到一个运行时错误,提示
无法将N/a分配给DateTime。
如何解决此问题?

您可以尝试以下代码:

if (userDataGridView.Rows[i].Cells[column].Value == null || userDataGridView.Rows[i].Cells[column].Value.ToString() == "")
                        {
                            userDataGridView.Rows[i].Cells[column].Value = userDataGridView.Rows[i].Cells[column].Value.ToString().Replace("", "N/A");
                        }

这将检查单元格是否为空,然后将其替换为N/A。但这是不可取的,因为db值为
DateTime
,如果您未能正确转换,这将在将来导致问题。因此,如果从db中提取空值,则可以将其显示为
N/a
,但当它存储回db时,应将其转换回
null
或实际的
DateTime

,无法检查
。ToString()==null
这本身将导致空引用异常,假设对象
.Value
本身为空。您需要检查对象是否有空引用
。Value==null
Oops my bad。忘了吧。谢谢@MickIlovski。编辑上面的代码。如何填充userDataGridView?