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

Validation of text input in GridViewComboBoxColumn

4 Answers 375 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Inger Marie
Top achievements
Rank 1
Inger Marie asked on 07 Jan 2011, 01:50 PM
When altering the celltemplate on a GridViewDataColumn, I do not get the expected values in GridViewCellValidatatingEventArgs in the CellValidating event.

My XAML looks like something this:

    <telerik:RadGridView Name="dayDriveLogGridView"
                             ItemsSource="{Binding DriveLog}"
                             AutoGenerateColumns="False"
                             Deleting="dayDriveLogGridView_Deleting"    
                             CellValidating="dayDriveLogGridView_CellValidating"
                             >

            <telerik:RadGridView.Columns>

   <telerik:GridViewDataColumn Name="colKilometers"
                                SortingState="Ascending"
                                DataMemberBinding="{Binding Path=Entity.Kilometers}"
                                CellStyle="{StaticResource DisableIfNotSettled}"
                       >
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="{localView:Translate Kilometers}" ToolTip="{localView:Translate DriveLogTooltip}" />
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>

    <telerik:GridViewDataColumn Name="colDestination"
                                       IsReadOnly="False"
                                       DataMemberBinding="{Binding Entity.Destination, Mode=TwoWay}"
                                       CellStyle="{StaticResource DisableIfNotSettled}"
                                       >
               <telerik:GridViewDataColumn.Header>
                   <TextBlock Text="{localView:Translate Destination}" ToolTip="{localView:Translate DriveLogTooltip}" />
               </telerik:GridViewDataColumn.Header>
               <telerik:GridViewDataColumn.CellEditTemplate>
                   <DataTemplate>
                       <ComboBox x:Name="destinationComboBox" ItemsSource="{Binding Path=PossibleDestinations}" IsEditable="True"
                                 LostFocus="DestinationComboBox_LostFocus"
                                 SelectedItem="{Binding Path=Entity.Destination,
                                           Mode=TwoWay}"
                                 >
                           <ComboBox.Text>
                               <Binding Path="Entity.Destination" Mode="TwoWay"
                                        >
                                   <Binding.ValidationRules>
                                       <model:DestinationValidationRule ValidatesOnTargetUpdated="True" />
                                   </Binding.ValidationRules>
                               </Binding>
                           </ComboBox.Text>                                
                       </ComboBox>
                   </DataTemplate>
               </telerik:GridViewDataColumn.CellEditTemplate>
           </telerik:GridViewDataColumn>

       </telerik:RadGridView.Columns>
        </telerik:RadGridView>

Question 1: In dayDriveLogGridView_CellValidating, I get the expected e.NewValue when changing Kilometers, but not when changing Destination (whenever I input any string, e.NewValue is still null). I'd like to handle all my validations the same way in the codebehind... but it seems like I have to dig out Destination from the EditingElement ???

Question 2: It seems like the DestinationValidationRule gets activated and gives the combobox a red border. But for some reason, this does not bubble out, making the cell invalidated. Can this be done elegantly in XAML? Or in the code-behind.

To me, it seems like that in order  to make validation, I need a way to hook the cell up with the combobox  (either the rule or the cellvalidation) go most smoothly. Did I miss something?

(I did read http://www.telerik.com/help/wpf/gridview-managing-data-validation.html )
The viewmodel has a property called DriveLog. DriveLog is an observable collections of DriveLogEntryViewModel - each with the property Entry of type DriveLogEntry. DrivLogEntry is a Linq2SQL entity (autogenerated).

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 12 Jan 2011, 05:35 PM
Hello Inger Marie,

Depending on the particular settings, once a CellEditTemplate is defined, the validation logic needs to be handled explicitly and you need to implement your custom logic for it. Generally, the validation is fired after editing and the error message popups on failing the required rules.
Furthermore, as I am not aware of the validation rules you implemented, I am not able to reproduce your exact scenario. May you provide a bit more information on it ? On the other hand, is it not appropriate for you to use the GridViewComboBoxColumn ? 
Still I am sending you a sample project that you can use as a reference. Feel free to change it in the way you want and send it back if necessary. Or else, provide me a bit more details so that I could simulate your scenario on this project.

 

Kind regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Inger Marie
Top achievements
Rank 1
answered on 13 Jan 2011, 08:31 AM
The rule for the destination is this: It must be supplied. It may be any string - those in the combobox are just the most likely destinations (current customers). However the true destination may be a new - not registered - customer, the baker, anything really.

DestinationValidationRule looks like this:

 public class DestinationValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            if (value == null)
                return new ValidationResult(false, "An error message");
            if(value is string && !String.IsNullOrWhiteSpace(value as string))
            {
                return new ValidationResult(true, null);
            }
            return new ValidationResult(false, "An error message");
        }
    }

Q1. When entering something in the destinationComboBox, the dayDriveLogGridView_CellValidating event gets triggered, but with e.NewValue set to null. I believe perhaps that has something to do with DataMemberBinding? But why is e.NewValue null then?

Q2. The GridViewComboBoxColumn has no IsEditable property as RadComboBox do. With a RadComboBox (and a 'normal' combobox). Why this difference?

Thanks
0
Maya
Telerik team
answered on 18 Jan 2011, 05:32 PM
Hello Inger Marie,

Generally, I would recommend you to use the GridViewComboBoxColumn so that you do not define a CellEditTemplate and apply a separate validation logic through rules.The GridViewComboBoxColumn has a IsComboBoxEditable property that may be set to "True", if you want to edit manually the item. 
As for the null value in the CellValidating event, it will be null if the entered value is not yet available in the ItemsSource of the ComboBox. 
 

Regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Inger Marie
Top achievements
Rank 1
answered on 19 Jan 2011, 01:07 PM

Thank you very much.
Tags
GridView
Asked by
Inger Marie
Top achievements
Rank 1
Answers by
Maya
Telerik team
Inger Marie
Top achievements
Rank 1
Share this question
or