Here is my situation:
public class MyClass {
[DisplayName("My Column")]
public decimal MyField { get; set; }
}
A collection (that implements IBindingList) of these classes are bound to a Grid. The grid has a manually created schema that defines MyField, and defines a condition format that should set the foreground red when the value is negative.
The problem is that the data type of the column in the designer defaults to String, and doesn't let me change it because it's a bound column. Subsequently it looks like the conditional formatting is trying to do a less than operation using strings, therefore comparing "-10" to "0", instead of -10 to 0.
The result of this is that the conditional formatting is never applied to negative values. If I change the conditional format to "Starts With", "-", which works, but this won't help me when I want to do things like comparing whether a decimal value is less than 5 (or any other arbitrary decimal value).
What am I doing wrong here? How can I do "real" conditional formatting based on the underlying data type of the property on the class, rather than the String data type that the manually created schema assumes?
Regards,
Mark
Mark,
I just tried this out by creating a class "MyClass" that has a public decimal property as well as a class "MyList" that derives from BindingList<MyClass>. At design-time I went to the DataSource property of the grid, chose "Add Project Data Source", selected Object, then chose "MyList" as the type. In the grid's designer I saw that the property of MyClass was a decimal type, and my conditional formatting worked appropriately. Did you follow any different steps that might have caused these properties to be incorrectly initialized? It's correct that you can't change the DataType of a bound column, but my point was that the .NET BindingManager seems to be getting incorrect information; are you certain that the property associated with the column is a decimal type?
-Matt
I have implemented InititalizeRow for the moment, it will iterate over the cells and set any <0 decimal columns to Red.
I am using the object data source (IBindingList<T>). The error message I get from the designer (Property) is...
---------------------------
Microsoft Visual Studio
The DataType cannot be set for bound columns.
OK
It doesn't seem like this is an issue with ConditionFormatting, but rather with how the grid is getting the structure of your grid through the BindingManager. Something is definitely wrong if the grid is picking up that a decimal column has a DataType of 'string', but it's difficult to say why this might be happening without seeing it in action; could you post a small sample so that I could look into it? Are you using an ObjectDataSource to bind the grid to a list of these objects? If you're manually defining the schema, you should be able to change the DataType of the column yourself.
A workaround is to handle your appearance resolution in the InitializeRow event, but of course this removes the design-time support of conditional formatting that you're trying to do.