Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Datagrid Validation while importing data

$
0
0

HI,

I have data grid and I want to implement data grid validation while importing data.
Functionality needs to be like this : If any cell of data grid has a more than 4 character of text then 
data grid rows header should have error icon.

I was able to achieve this when I edit cell and at the end of editing I can see that Icon if length of cell text is more than
4. But I want to test that while importing or after all data has been imported. And error Icon has to be there until I fixed it.                                                             
Here is code:
 <loc:RhinoDataGrid Grid.Row="0"  VirtualizingStackPanel.IsVirtualizing="False" ItemsSource="{Binding DataTable}">

            <DataGrid.RowValidationRules>
                <loc:CourseValidationRule ValidationStep="UpdatedValue"/>
            </DataGrid.RowValidationRules>

            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <Grid Margin="0,-2,0,-2">
                        <Ellipse StrokeThickness="0" Fill="Red" 
              Width="{TemplateBinding FontSize}" 
              Height="{TemplateBinding FontSize}" />
                        <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
              FontWeight="Bold" Foreground="White" 
              HorizontalAlignment="Center"  />
                    </Grid>
                </ControlTemplate>
            </DataGrid.RowValidationErrorTemplate>
        </loc:RhinoDataGrid>

public class CourseValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value,
            System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult vResult = ValidationResult.ValidResult;
            DataRowView drView = (value as BindingGroup).Items[0] as DataRowView;
            for (int i = 0; i < drView.Row.ItemArray.Count(); i++)
            {
                string strVal = drView.Row.ItemArray[i].ToString();
                if (strVal.Length > 4)
                {
                    vResult = new ValidationResult(false,
                    "Start Date must be earlier than End Date.");
                    break;
                }
            }
            return vResult;
        }
    }      

In this case it should show error icon on all rows

Thanks

Dee

          

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>