Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
560
How to validate Cells programmatically?
posted

I have a Grid, some Fields in the Grid have custom validation which are set in the FieldLayout. For the beginning i will edit a cell, and set a new value for example -4   In the EditModeEnding EventHandler i check the value, but i couldn´t set the HasDataError  because it is a readonly property. 

What is the best way to validate a specific Field and color the background with a color ?
In the Feature Browser there is an example Data Error Styling, that is the behaviour which i will implement. But in the example the DataSource implements some interfaces and in my case, i have no DataSource which implements this interface.

 

XAML.

<my:Field Name="Name"  Label="Name">
</my:Field>

 <my:Field Name="value"  Label="value">
     </my:Field>

 <igDP:FieldLayoutSettings SupportDataErrorInfo="RecordsAndCells"
            DataErrorDisplayMode="ErrorIconAndHighlight" />

 

 

Codebehind:

void xamDataPresenter1_EditModeEnding(object sender, EditModeEndingEventArgs e)
        {
            if (e.Cell.Field.Label.Equals("man"))
            {
                int value;

                if (!Int32.TryParse(e.Editor.Text, out value))
                {
                    e.AcceptChanges = false;

                    string val = e.Cell.Value.ToString();

                    MessageBox.Show("Data Validation Failed. Must enter an Integer: " + val);

                    e.Cancel = true;
                }
                else
                {
                    if(value < 0)
                    {
                       // e.Cell.HasDataError    TODO !!! How to set this cell ??
                   
                        e.Cancel = true;

                       // TODO: assign this cell with a different style   -> RED Background 
e.Cell.DataPresenter.FieldSettings.CellValuePresenterStyle = GetLabelStyle(System.Windows.Media.Brushes.Red, true);             

 

                    else
                    {          //assign different style   -> GREENBackground  

                     }


                }
            }
        }

 

The next step, how  to set the cell the Backgrounds programmatically (GREEN, RED)

 

 

In the examples i have seen a DataSource Class which implements the IDataErrorInfo. In my project we have Bound all Fields manually in the grid. i have no IDataErrorInfo implemented, because i haven´t got a BusinessClass. how to use this functionality without IDataErrorInfo in my project?

Parents
No Data
Reply
  • 2197
    posted

    Hello.

    If you don't want to implement IDataErrorInfo, then the easiest way to enforce validation is to use a ValueConstraint on the cells editor. For example, let's say that you have a UnitCost field of type decimal and you don't want the user to be able to enter a cost that is less than zero. You can create a ValueConstraint on the editor which will prevent the user from ever being able to enter invalid data in the first place.

    When you do this, you cannot enter a value that is less than zero. You also cannot enter any alpha characters; only numeric entry is allowed. This is because the EditAsType property on the UnitCost field is set to sys:decimal.

    Using a ValueConstraint negates the need to handle EditModeEnding and display a message box telling the user to enter a number. You're guaranteed to get valid data every time with this method with a less code.

     

     

     

     

     

     

     

    <

     

     

    igDP:Field Name="UnitCost" Label="Unit Cost">

     

     

     

     

    <igDP:Field.Settings>

     

     

     

     

    <igDP:FieldSettings

     

     

     

    EditorType="{x:Type igEditors:XamNumericEditor}"

     

     

     

    EditorStyle="{StaticResource UnitCostEditorStyle}"

     

     

     

    EditAsType="{x:Type sys:Decimal}"/>

     

     

     

     

    </igDP:Field.Settings>

     

     

     

     

    </igDP:Field>

    <

     

     

    Style x:Key="UnitCostEditorStyle" TargetType="{x:Type igEditors:XamNumericEditor}">

     

     

     

    <Setter Property="Format" Value="$###,##0.00;($###,##0.00)"/>

     

     

     

    <Setter Property="ValueConstraint">

     

     

     

    <Setter.Value>

     

     

     

    <igEditors:ValueConstraint MinInclusive="0.00"/>

     

     

     

    </Setter.Value>

     

     

     

    </Setter>

     

     

     

    </Style>

    Sincerely,
    Charlie
    Senior Developer Support Engineer
    MCTS

Children
No Data