CSLA Validation for Grid...error provider not blinking in grid cells.

CSLA Validation for Grid...error provider not blinking in grid cells.

Old forum URL: forums.lhotka.net/forums/t/2414.aspx


Bhupesh posted on Friday, February 23, 2007

Hi,

I am having some problems validating the grid using CSLA validation rules. I've included the necessary steps related to validation, which I think can be. The steps include:
1) In the property named CustomerName in Customer class I've included the following two   lines:
- PropertyHasChanged(CustomerName);
- ValidationRules.CheckRules(CustomerName);

2) Then added the following validation rules in the same class:
- ValidationRules.AddRule(new RuleHandler(CommonRules.StringRequired),    CustomerName);
- ValidationRules.AddRule(CommonRules.StringMaxLength, new     Csla.Validation.CommonRules.MaxLengthRuleArgs(CustomerName, 50));

3) Assigned the grid's Error on errorProvider1 to errorProvider1

4) Then for the grid added the following code:
   grdCustomer.DataMember = "CustomerList";
   grdCustomer.DataSource = _formBindingSource; //_formBindingSource an object of       //BindingSource

   errorProvider1.DataSource = _formBindingSource;
   errorProvider1.DataMember = "CustomerList";

But still I am unable to show the error provider in the grid cells despite when the rule breaks when the customer name is left blank or the specified characters exceeds 50.
Though when the code is run by adding the watch window...it shows the rule broken and the message showing customer name required. Also IsValid() returns false.
   Do I've to include more steps in this or have to include a custom validation rule handler?
        Seeking help in this matter.

Thanks,
Bhupesh

RockfordLhotka replied on Tuesday, February 27, 2007

I suggest you review Chapter 9 to see how the binding is handled.

You DO NOT need to call CheckRules() in a property set - that is already handled by PropertyHasChanged(), and you are just causing the rules to be checked twice, which is a waste.

You DO NOT need to use an ErrorProvider control with a grid. It won't actually do anything, because the grid control handles its own error status. ErrorProvider only works against things like TextBox.

I suggest using drag-and-drop from the Data Sources window in Visual Studio to set up your bindings. That's far simpler than trying to do it by hand. I suspect that you are not correctly setting the DataSource properties for the grid or your bindingsource - or perhaps you are doing them in the wrong order.

Even if you don't want to use drag-and-drop binding in your actual app (though I don't know why you wouldn't), I suggest you use it in a sample app and look at the resulting code - that will show you the correct way to set up the bindingsource and binding to the grid control.

Bhupesh replied on Wednesday, March 28, 2007

Hi Rocky,

                          Thanks for your reply. I m pretty much sorry for replying late to your reply. Actually your concepts and tips regarding databinding and CheckRules hepled a lot. Also I was using the thirdparty control grid. So had to change some properties for the grid to support data error handling in that. So its running perfectly all right now.

 

Thanks & Regards,

Bhupesh

Copyright (c) Marimer LLC