C# 在设计时将 DataGridView.DefaultCellStyle.NullValue 设置为 null 会在添加行运行时引发错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:00:31  来源:igfitidea点击:

Setting DataGridView.DefaultCellStyle.NullValue to null at designtime raises error at adding rows runtime

提问by nandras

In Visual Studio 2008

在 Visual Studio 2008 中

  • add a new DataGridView to a form
  • Edit Columns
  • Add a a new DataGridViewImageColumn
  • Open the CellStyle Builder of this column (DefaultCellStyle property)
  • Change the NullValue from System.Drawing.Bitmap to null
  • Try to add a new Row to the DataGridView at runtime (dataGridView1.Rows.Add();)
  • You get this error: System.FormatException: Formatted value of the cell has a wrong type.
  • 向表单添加一个新的 DataGridView
  • 编辑列
  • 添加一个新的 DataGridViewImageColumn
  • 打开此列的 CellStyle Builder(DefaultCellStyle 属性)
  • 将 NullValue 从 System.Drawing.Bitmap 更改为 null
  • 尝试在运行时向 DataGridView 添加一个新 Row (dataGridView1.Rows.Add();)
  • 您收到此错误: System.FormatException: 单元格的格式化值类型错误。

If you change back the NullValue to System.Drawing.Bitmap (as it was) you still get the same error at adding a row.

如果您将 NullValue 改回 System.Drawing.Bitmap(原样),您在添加行时仍会遇到相同的错误。

If you set the NullValue at runtime instead of designtime you don't get anny error. (dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;)

如果在运行时而不是设计时设置 NullValue,则不会出现 anny 错误。(dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;)

Could you tell me why is that?

你能告诉我这是为什么吗?

采纳答案by ljs

This may well be a bug in the designer; if you take a look around at the .designer.cs file (maybe doing a diff from before and after you set NullValue to null) you should be able to see the code it generates.

这很可能是设计器中的一个错误;如果您查看 .designer.cs 文件(可能会在将 NullValue 设置为 null 之前和之后进行比较),您应该能够看到它生成的代码。

回答by nandras

Kronoz is right. After setting it at designtime it adds this to the .designer.cs:

克罗诺兹是对的。在设计时设置它后,它将它添加到 .designer.cs 中:

dataGridViewCellStyle1.NullValue = "null";

dataGridViewCellStyle1.NullValue = "null";

If I modify "null"to nullthen it works fine. I checked the DataGridViewCellStyle.NullValue set_NullValue(Object) and get_NullValue with reflector and I think that a string value shouldn't raise any error here.

如果我将“null”修改为null,则它可以正常工作。我用反射器检查了 DataGridViewCellStyle.NullValue set_NullValue(Object) 和 get_NullValue,我认为字符串值不应该在这里引发任何错误。

Anyway be careful with this and if you want to set it designtime then don't forget to modify the .design.cs.

无论如何要小心这一点,如果你想设置它的设计时间,那么不要忘记修改.design.cs。

回答by Thies

Change the NullValue from System.Drawing.Bitmap to null

将 NullValue 从 System.Drawing.Bitmap 更改为 null

When you enter 'null' into the field for NullValue in the Designer, you are specifing the string value "null". The only way to set NullValue to a non-string value is to set it programmatically or by modifing the designer code yourself.

当您在 Designer 的 NullValue 字段中输入“null”时,您指定的是字符串值“null”。将 NullValue 设置为非字符串值的唯一方法是以编程方式或通过自己修改设计器代码来设置它。

回答by Thies

I found that its better if you just delete the item from the designer all together from the Format area and the default null value area. Then it sets it back to the real null. I'm going to try to set it in the init section away from the designer generated crap.

我发现如果您只是从格式区域和默认空值区域一起从设计器中删除项目,它会更好。然后它将它设置回真正的空值。我将尝试将它设置在 init 部分,远离设计师生成的废话。

回答by VipX1

Checkbox can't have a String value. Don't set any default value in the IDE properties dialog. I had "Empty" written into the RowsDefaultCellStyle.Format property and that caused the error. It was self inflicted. As a fix I was trying to set the checkbox state to unchecked but I just needed to delete the string value.

复选框不能有字符串值。不要在 IDE 属性对话框中设置任何默认值。我在 RowsDefaultCellStyle.Format 属性中写入了“Empty”,这导致了错误。这是自己造成的。作为修复,我试图将复选框状态设置为未选中,但我只需要删除字符串值。