• North American Sales: 1-800-231-8588
  • Global Contacts
  • My Account
Infragistics Infragistics
Menu
  • North American Sales: 1-800-321-8588
  • My Account
    • Sign In/Register
  • Design & DevelopmentDesign & Develop
    • Best Value
      Infragistics Ultimate The complete toolkit for building high performing web, mobile and desktop apps.
      Indigo.Design Use a unified platform for visual design, UX prototyping, code generation and application development.
    • Web
      Ignite UI for Angular Ignite UI for JavaScript Ignite UI for React Ultimate UI for ASP.NET Indigo.Design
    • Desktop
      Ultimate UI for Windows Forms Ultimate UI for WPF
      Prototyping
      Indigo.Design
    • Mobile
      Ultimate UI for Xamarin Ultimate UI for iOS Ultimate UI for Android
    • Automated Testing Tools
      Test Automation for Micro Focus UFT: Windows Forms Test Automation for Micro Focus UFT: WPF Test Automation for IBM RFT: Windows Forms
  • UX
    • Indigo.Design Desktop Collaborative prototyping and remote usability testing for UX & usability professionals
    • Indigo.Design A Unified Platform for Visual Design, UX Prototyping, Code Generation, and App Development
  • Business Intelligence
    • Reveal Embedded Accelerate your time to market with powerful, beautiful dashboards into your apps
    • Reveal App Empower everyone in your organization to use data to make smarter business decisions
  • Team Productivity
  • Learn & Support Support
    • Help & Support Documents
    • Blogs
    • Forums
    • Product Ideas
    • Reference Applications
    • Customer Stories
    • Webinars
    • eBook & Whitepapers
    • Events
  • Free Trials
  • Pricing
    • Product Pricing / Buy Online
    • Renew Existing License
    • Contact Us
WPF
  • Product Platforms
  • More
WPF
WPF Validation in the XamDataGrid
  • Blog
  • Files
  • Wiki
  • Mentions
  • Tags
  • More
  • Cancel
  • New
WPF requires membership for participation - click to join
  • WPF
  • Configuring the XamTab Control
  • Creating a Custom Summary for the XamDataGrid
  • Defining a Custom Path in the XamCarousel
  • Enabling Row Summaries in the XamDataGrid
  • Exporting the XamDataGrid to Excel
  • Hosting a WPF Control in a Windows Forms Application
  • Printing the XamDataGrid with Infragistics.Reports
  • Spell Checking in the XamDataGrid
  • Tangerine -- A WPF Reference Application
  • Using the Infragistics WinGrid in a WPF Application
  • Validation in the XamDataGrid
  • XamDataGrid :: Copying to Excel via the Clipboard
  • XML Databinding with the XamDataGrid

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

  • WPF
  • XamDataGrid
  • Share
  • History
  • More
  • Cancel
Related
Recommended