Hello,
I have a grid wiht a column where the Style = Integer. The property on the DataOsurce is int too, so everything should work fine.
At runtime, the grid displays the default value of zero. Then I edit it and set it to blank. When I tab out I get an error thrown by the grid "Object of type 'System.DBNull' cannot be converted to type 'System.Int32". I guess empty text is interpreted as DBNull.Value, which can not be converted to an Int32. I tried setting Nullable = Disallow, with no success.
Is there a way to not allow entering blank (null) values on a numeric column?
As a side note, I think the Nullable property should provide an option "TypeDefault". When selected, blank text should be converted to defaullt(T), where T is the data type of the column.
One more thing: I like the new design of the forums, but there is no forum-specific search (or I haven't found it?), only global. On a global search, only one page of results are displayed, there's no links to fetch more results.
Mike, Magued:
Thanks for the answer. A DataFilter can solve the problem, but I just found a simpler way. Setting the Column.Nullable property to Nothing does the trick.
I tried with int, double and DateTime. When tabbing out of a blank cell the value seems to revert back to default(T), where T is the data type of the column.
This is not very intuitive, and the documentation doesn't say anything about value types:
I think it is more clear if there's a new value in the Nullable enum "TypeDefault", that returns the default(T) when the cell is blank.
DataFilter is a good idea and clean way as Mike suggested. The following is a KnowledgeBase article on how to implement a DataFilter:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=5014
Magued
The grid will never automatically convert a blank into a 0. It might convert a blank into a null or a DBNull (based on the Nullable property of the column). But never 0.
So if this is happening, it must the data source that is doing it.
In your case, though, since you are using a custom class, your data source cannot accept a null, since the field is an Int.
You can get around this in a number of ways. One way would be to allow your data source to accept nulls by changing the field to a nullable type. Then you could trap the setting and if someone tries to set the Value to null, return 0.
Another way handle this would be to use a DataFilter in the grid.
Hello Mike,
My whole point is that a blank value is throwing an error. I'm not sure who is throwing the error, but if the column Style is Interger and the user changes the text to blank, I would expect the value to revert back to 0.
On the previous reply Magues sent an example binding the grid to a DataSet. In that case a blank taxt is reverted back to 0. Then I posted an example using a BindingSource binded to a List of Business objects. In my example, when the text is blank the value is not reverted to 0, and I get the error instead.
Is that the expected behavior?
Thanks.
Okay, now I am really confused. :)
You cannot store a blank in an Int value. Blank is not a valid Int. So.. if you don't want an error, then what exactly do you want the behavior to be?
If you just want to prevent the error message from displaying, then you can do this in the CellDataError event of the grid. The event args allow you to cancel the default error message. I'm not entirely sure what happens to the Value of the cell at that point - it probably reverts to it's original value.