Cell and column validation in DataGridView?

Apr 1, 2008 at 7:41 PM
I have business object A, which has a BindingList of object B. Object B has a property called Proportion. I have a DataGridView that uses the BindingList<B> as its data source. Now what I'm trying to accomplish in terms of validation is have the "Proportion" column be subject to two constraints:

1. Each cell in the column should be between 0-1. This I easily accomplish by decorating the business object's Proportion property with the required RangeRuleAttribute(0,1,ErrorMessage="xxxx")
2. Have all the cells in the column sum to 1.0.

I've thought/investigated these methods:

1. Add a CustomValidationRule to the BindingList in object A. The first issue is that editing of the DataGridView data does not cause the set method for the property exponsed in Object A to fire. One workaround is to attach a handler to the DataGridView's RowValidated event, such that after cell/row validation has occurred, manually call validation on the BindingList property. Another problem is this requires an external error provider control to display the results. Ideally I would like the error icon to appear either at the cell which last caused the proportions to no longer equal 1.0, or at the column header for entire proportion column. Another issue is that this error provider has limited alignment options on the DataGridView. I've settled on TopRight, with a negative padding to place the error icon inside the DGV.

2. If I could get individual cell validation for proportion to handle this, then the error icon would display in a better location for the user. But since my objects are created from xml deserialization, the individual objects in the BindingList have no way to reference the BindingList or other objects in the list.

3. Last resort handle this manually by hooking into cell validation for that column?

Apr 2, 2008 at 10:50 PM

If you send me a sample app of what you are trying to achieve I will have a look into it.
Apr 3, 2008 at 12:01 PM
Edited Apr 7, 2008 at 11:20 AM

Thank you, I will. I am putting together a small sample app right now.