Infragistics Article

Validation in the XamDataGrid

Validation in the XamDataGrid is currently handled through the ValueConstraint class off of the Editors.  Using this class you can do things like set a trigger to change the border of a cell to red when an invalid value it entered.  This article shows off how to use the ValueConstraints class. 

The first thing to understand is that there are multiple types of constraints and they include the following:

  • FixedValue Condition – Does the value equal this?
  • Enumeration Condition – An Object implementing IEnumerable which contains a list of values.
  • MaxExclusive Condition – The value must be less than this.
  • MaxInclusive Condition – The max value that the constrained value can be.
  • MinExclusive Condition – The value must be greater than this.
  • MinInclusive Condition – The min value that the constrained value can be.
  • Min/MaxLength Condition – The length of type string must be greater/less than this.
  • Nullable Condition – Whether the constrained value can be null.
  • RegExPattern Condition – A regular expression for the value.

 

Using BindToSampleData property on the XamDataGrid to practice validation.

Enumeration of Fixed Values:

<igDP:Field Name="department" Label="Department">
    <igDP:Field.Settings>
        <igDP:FieldSettings EditorType="{x:Type igEdit:XamTextEditor}">
            <igDP:FieldSettings.EditorStyle>
                <Style TargetType="{x:Type igEdit:ValueEditor}">
                    <Style.Resources>
                        <x:Array Type="sys:String" x:Key="FixedValue">
                            <sys:String>Admin</sys:String>
                            <sys:String>Sales</sys:String>
                            <sys:String>Human Resources</sys:String>
                        </x:Array>
                    </Style.Resources>
                    <Style.Setters>
                        <Setter Property="ValueConstraint">
                            <Setter.Value>
                                <igEdit:ValueConstraint Enumeration="{StaticResource FixedValue}" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="InvalidValueBehavior" Value="RetainValue" />
                    </Style.Setters>
                    <Style.Triggers>
                        <Trigger Property="IsValueValid" Value="false">
                            <Trigger.Setters>
                                <Setter Property="BorderBrush" Value="Red" />
                            </Trigger.Setters>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </igDP:FieldSettings.EditorStyle>
        </igDP:FieldSettings>
    </igDP:Field.Settings>
</igDP:Field>

 

Max/Min Inclusive:

<igDP:Field Name="salary" Label="Salary">
    <igDP:Field.Settings>
        <igDP:FieldSettings EditorType="{x:Type igEdit:XamCurrencyEditor}" EditAsType="{x:Type sys:Double}">
            <igDP:FieldSettings.EditorStyle>
                <Style TargetType="{x:Type igEdit:ValueEditor}">
                    <Style.Setters>
                        <Setter Property="ValueConstraint">
                            <Setter.Value>
                                <igEdit:ValueConstraint MinInclusive="0" MaxInclusive="100000" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="InvalidValueBehavior" Value="RetainValue" />
                    </Style.Setters>
                    <Style.Triggers>
                        <Trigger Property="IsValueValid" Value="false">
                            <Trigger.Setters>
                                <Setter Property="BorderBrush" Value="Red" />
                            </Trigger.Setters>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </igDP:FieldSettings.EditorStyle>
        </igDP:FieldSettings>
    </igDP:Field.Settings>
</igDP:Field>

Reg-Ex for an E-Mail:

<igDP:Field Name="email" Label="E-Mail">
    <igDP:Field.Settings>
        <igDP:FieldSettings EditorType="{x:Type igEdit:XamTextEditor}">
            <igDP:FieldSettings.EditorStyle>
                <Style TargetType="{x:Type igEdit:ValueEditor}">
                    <Style.Setters>
                        <Setter Property="ValueConstraint">
                            <Setter.Value>
                                <igEdit:ValueConstraint RegexPattern="^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="InvalidValueBehavior" Value="RetainValue" />
                    </Style.Setters>
                    <Style.Triggers>
                        <Trigger Property="IsValueValid" Value="false">
                            <Trigger.Setters>
                                <Setter Property="BorderBrush" Value="Red" />
                            </Trigger.Setters>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </igDP:FieldSettings.EditorStyle>
        </igDP:FieldSettings>
    </igDP:Field.Settings>
</igDP:Field>

 

EditModeValidationError Event

So, your user has failed the validation logic that you’ve put in place.  When this happens the EditModeValidationError Event fires and allows you to perform some action.  For instance, you can configure the error message as follows:

private void xamDataGrid1_EditModeValidationError(object sender, Infragistics.Windows.DataPresenter.Events.EditModeValidationErrorEventArgs e)
{
    switch (e.Cell.Field.Name.ToString())
    {
        case "department":
            e.ErrorMessage = "That department does not exist!  Please enter a valid department."
            break;
        case "salary":
            e.ErrorMessage = "Salary cannot be more than $100,000.00";
            break;
        case "email":
            e.ErrorMessage = "Please enter an e-mail address (e.g. __@__.com)";
            break;
    }
}

Conclusion

That wraps up this article on validation in the XamDataGrid.  Now keep in mind that this works with all of our editors as well; so you’re not…constrained…to the XamDataGrid to use these capabilities.  You can grab the code for this sample here.

Validation

Recent Comments

By: Deepalakshmi Posted on 04-26-2012 5:49 AM

Hi,

can anyone solve my problem?

i have infragistics xamgrid,in this grid i want enterable focus that means we can enter data from one cell to another cell.its  default tab focus is working but in my project we need enterable grid.how its possible in this?

By: Deepalakshmi Posted on 04-26-2012 5:44 AM

Hi,

Any one can you clarify my doubt

infragistics xamgrid i have 4 columns. first column is combobox remainings  columns are textblock. if i select any one data from combobox means its ill fetch all data from table and automatically filling for remaining columns. how its possible in this grid?because here no options for combobox.selecteditem and selectionchanged event. then how?

By: liu Posted on 03-08-2012 9:11 AM

楼主辛苦了!学习了.

It is very usefull to me!

By: csjasnoch Posted on 05-27-2011 2:18 PM

I can't believe you actually call this an "Article". Where is the material? All you have is a list of conditions and a couple snippets of code. That does not qualify as an article by any standard.

By: KarleATNeufra Posted on 11-22-2010 3:46 AM

Well, i miss the possibility to implement custom validation rules for example by implementing a interface.

You have used such strategie at other places too (=> i.e. summary rows in the grid) and i think you really should support it for validation rules because if there are conditions in the rules the implementation gets very tricky and is hard to encapsulate.

By: Milama Posted on 10-03-2010 4:00 AM

It is very hard to understand.

custom-paper-writing.com/admission-essay-service

By: zhililin Posted on 07-13-2010 7:05 PM

I am new to WPF Infragistics

I had an numerical cell, I give a constrain of minimum and maximum 0-10. It worked fine. But if user delete the value, I got message "please set a value". I want to give a more specific message like: value can't be empty.

So I featured that empty is not a valide value, If I implement xamDataGrid_EditModeValidationError as suggested in this article, I could route the message to the one I want to display.

Here is my implementation:

In  the Xaml file:

        <DataPresenter:XamDataGrid

                               Theme="Office2k7Blue"

                               AutoFit="True"

EditModeValidationError="xamDataGrid_EditModeValidationError">

But when I run, xamDataGrid_EditModeValidationError never gets called. The invalise entry in the active cell never trigger the event handler:

xamDataGrid_EditModeValidationError

Any suggestion?

Julie

By: Ryan Posted on 09-17-2009 10:34 AM

How does one make a field/cell required (NOT Nullable)?  The RegEx & MinLength does not work in all cases.

Sign in to post a comment to this article.