How to perform validation within editable WinForm DataGridView

Topics: Validation Application Block
May 13, 2008 at 4:15 PM
Seems not able to bind to a set the property for DataGrid column. Is there any samples?
May 13, 2008 at 6:57 PM

I’m tiring to do validations on DataGridView to, any suggestions??


May 13, 2008 at 10:11 PM


The supplied ValidationProvider does not support validating DataGridView cells. There are several reasons for this:

Implementing a new DataGridView based validation provider is not very hard, assuming the column names match the source properties' names. This provider would work as an IExtenderProvider like the existing ValidationProvider but only for DataGridView instances. It would attach a handler for the CellValidating event, and it would invoke the ValidationIntegrationHelper to hook in the validation infrastructure. The event handler would look like this (from a throwaway implementation I just coded):



void OnCellValidating(object source, DataGridViewCellValidatingEventArgs e)




if (PerformValidation)




if (control.Rows[e.RowIndex].IsNewRow) { return; }


control.Rows[e.RowIndex].ErrorText =




ValidatedCellIntegrationProxy validationProxy



new ValidatedCellIntegrationProxy(








ValidationIntegrationHelper helper = null;









helper =

new ValidationIntegrationHelper(validationProxy);




catch (InvalidOperationException)




// ignore missing property










Validator validator = helper.GetValidator();



ValidationResults validationResults = validator.Validate(validationProxy);



if (!validationResults.IsValid)




StringBuilder builder = new StringBuilder();



foreach (ValidationResult result in validationResults)





e.Cancel =



control.Rows[e.RowIndex].ErrorText = builder.ToString();




I'll make this implementation available through entlib contrib.


May 15, 2008 at 10:19 PM
Thaks, Fernando

I was trying to implement you code & I have  2 questions

What is ValidatedCellIntegrationProxy??

I supose that control  is refered to the DataGridView??

gracias denuevo
Juan Pablo
Sep 18, 2010 at 8:12 PM
Edited Sep 22, 2010 at 4:04 PM
The example above didn't work for me. I was able to create a new solution that I feel may be cleaner...
// ... call the method for DataGridView 
//     CellValidating and CellEndEdit events ...

this.dataGridView1.CellValidating += 
    (s, e) => ValidateCell(e.RowIndex);

this.dataGridView1.CellEndEdit += 
    (s, e) => ValidateCell(e.RowIndex);

// ... in the method below, I am using a class 
//   called "BindTest" and the DataGridView 
//   is bound to an instance of 
//   "ObservableCollection<BindTest>"

// my "BindTest" class has validation attributes 
//   from both the Data Annotations framework 
//   and Ent. Lib's validation framework.

        private void ValidateCell(int rowIx)
            // get the list
            var list = (ObservableCollection<BindTest>)this.dataGridView1.DataSource;
            // get the item
            var item = (BindTest)list[rowIx];
            // create validator of the object's type
            var valid = ValidationFactory.CreateValidator(item.GetType());
            // validate the item
            var results = valid.Validate(item);
            var row = this.dataGridView1.Rows[rowIx];

            // if not valid, show error
            if (!results.IsValid)
                // get the first result's error message
                var msg = results.ToList()[0].Message;
                // set the row's error
                row.ErrorText = msg;
                // optionally cancel the edit, 
                //    forcing the user to enter a valid value
                // e.Cancel = true;
                // object is valid, clear the error
                row.ErrorText = string.Empty;