This is a migrated thread and some comments may be shown as answers.

RowValidating anyhow raising a change in SelectedItem ?

2 Answers 185 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 20 Apr 2018, 03:31 PM

Hi ,

i using MVVM and the problem i have is i capture RowValidating  Event which is routedto a Method OnValidatingRow(arg)

 

DoValidatingRow = new DelegateCommand<object>(OnValidatingRow);

there i checkif valid or not and setting in some cases evnt.IsValid = false;

public override void OnValidatingRow(object arg)
        {
            var evnt = (GridViewRowValidatingEventArgs)arg;

    evnt.IsValid = false;

 

 

 

 

 

<telerik:RadGridView x:Name="LookupListGridCtrl" ItemsSource="{Binding Nodes, Mode=TwoWay}" SelectedItem="{Binding Nodes.SelectedUiDataItem,Mode=TwoWay}"  Grid.Row="1"
                             AutoGenerateColumns="false" GroupRenderMode="Flat" NewRowPosition="Top"  SelectionMode="Single" SelectionUnit="FullRow"
                             CanUserDeleteRows="True" ScrollMode="Deferred" IsSynchronizedWithCurrentItem="True" CanUserResizeColumns="True" CanUserSearch="True"
                             RowIndicatorVisibility="Visible" Margin="0,0,0,1" ValidationType="Default">

            <telerik:EventToCommandBehavior.EventBindings>
                <telerik:EventBinding Command="{Binding CommandProvider.DoDeleteRow}" EventName="Deleted" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoInsertRow}" EventName="RowEditEnded" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoAddNew}" EventName="AddingNewDataItem" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoValidatingCell}" EventName="CellValidating" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoValidatingRow}" EventName="RowValidating" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
            </telerik:EventToCommandBehavior.EventBindings>

2 Answers, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 20 Apr 2018, 04:08 PM

Sorry to fast pushed the Button by Return. :(

here again.

Hi ,

i using MVVM and the problem i have is that i capture RowValidating  Event which is routed to a Method OnValidatingRow(arg)

by (  Prism.Commands )

       DoValidatingRow = new DelegateCommand<object>(OnValidatingRow);

there i check if valid or not and setting in some cases evnt.IsValid = false;

       public override void OnValidatingRow(object arg)
       {
            var evnt = (GridViewRowValidatingEventArgs)arg;

              ....

                            GridViewCellValidationResult validationResult = new GridViewCellValidationResult();
                            validationResult.PropertyName = keys[0].ToString();
                            validationResult.ErrorMessage = $"{string.Join(",",keys)} is/are the unique Key(s) and already exist !";
                            evnt.ValidationResults.Add(validationResult);
                            evnt.IsValid = false;

       }

So all of this works well the invalid Row goes red( pic 2 )

BUT in the GridView the Event was raised because i changed an attribute and selected a different row so this raised the RowValidating but also after this

(anyhow if i set it evnt.IsValid = false; or not ) to the SelectedItem  changed to the new different row.(pic 2 marked !)

Problem is that i do not want that the different Row should bge selected it should stay selected in the Row which is invalid and i thought this is the reason to set IsValid = false. 

I know i can also catch the SelectionChanging Event and set there cancel return value = false but than i had to do the validation checks there twice ?

So please help how could i avoid this double checking. I hope it´s not necessary.

thanks br

thomas cinatl

here is the header of the Xaml definition of RadGridView ....

 <telerik:RadGridView x:Name="LookupListGridCtrl" ItemsSource="{Binding Nodes, Mode=TwoWay}" SelectedItem="{Binding Nodes.SelectedUiDataItem,Mode=TwoWay}"  Grid.Row="1"
                             AutoGenerateColumns="false" GroupRenderMode="Flat" NewRowPosition="Top"  SelectionMode="Single" SelectionUnit="FullRow"
                             CanUserDeleteRows="True" ScrollMode="Deferred" IsSynchronizedWithCurrentItem="True" CanUserResizeColumns="True" CanUserSearch="True"
                             RowIndicatorVisibility="Visible" Margin="0,0,0,1" ValidationType="Default">

            <telerik:EventToCommandBehavior.EventBindings>
                <telerik:EventBinding Command="{Binding CommandProvider.DoDeleteRow}" EventName="Deleted" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoInsertRow}" EventName="RowEditEnded" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoAddNew}" EventName="AddingNewDataItem" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoValidatingCell}" EventName="CellValidating" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
                <telerik:EventBinding Command="{Binding CommandProvider.DoValidatingRow}" EventName="RowValidating" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" />
            </telerik:EventToCommandBehavior.EventBindings>

 

0
Dilyan Traykov
Telerik team
answered on 25 Apr 2018, 11:33 AM
Hello Thomas,

As validation can be performed in view mode as well as in edit mode, we do not wish to limit the selection of the control when invalid data is present.

The suggested approach, as you've already found out is to handle and cancel the SelectionChanging event. Here's a generic approach which does not require the validation logic to be executed a second time:

private void RadGridView_SelectionChanging(object sender, SelectionChangingEventArgs e)
{
    var grid = sender as RadGridView;
    foreach (var item in e.RemovedItems)
    {
        var row = grid.GetRowForItem(item);
        if (!row.IsValid)
        {
            grid.ScrollIntoViewAsync(item, null);
            e.Cancel = true;
        }
    }
}

Please let me know if this would work for you.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or