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

Need gridview help quickly

5 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 21 Jun 2013, 08:53 PM
I have a RadGridView setup as follows:

                            <tk:RadGridView Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Height="175" Margin="0,5,0,10" Width="960" 
                                            AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserInsertRows="False" 
                                            CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSelect="True" CanUserSortColumns="False" 
                                            HorizontalAlignment="Left" IsFilteringAllowed="False" ItemsSource="{Binding EquipmentToAdd}" MinHeight="150" 
                                            RowIndicatorVisibility="Collapsed" SelectionUnit="Cell" ShowGroupPanel="False" VerticalAlignment="Bottom" ActionOnLostFocus="CommitEdit">
                                <i:Interaction.Behaviors>
                                    <cv:CellValidationBehavior />
                                    <cf:EquipmentQuickEntryGrid/>
                                </i:Interaction.Behaviors>

                                <tk:RadGridView.Columns>
                                    <tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=Count}" Header="Count" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="110" DataMemberBinding="{Binding Path=BaseEquipmentType}" Header="Base Type" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=EquipmentType}" Header="Equipment Type" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="130" DataMemberBinding="{Binding Path=Manufacturer}" Header="Manufacturer" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=Model}" Header="Model" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=AssetNumber}" Header="Asset Number" IsReadOnly="True" />
                                    <tk:GridViewDataColumn Width="150" Header="Serial Number" CellTemplateSelector="{StaticResource isAssetInUseSerialNumberTemplateSelector}" UniqueName="SerialNumber" IsReadOnlyBinding="{Binding IsInUse}"/>
                                    <tk:GridViewDataColumn Width="55"  Header="Remove" CellTemplateSelector="{StaticResource isAssetInUseDeleteTemplateSelector}" IsReadOnlyBinding="{Binding IsInUse}" />
                                    <tk:GridViewDataColumn Width="50" DataMemberBinding="{Binding Path=IsInUse}" IsVisible="False" />
                                </tk:RadGridView.Columns>
                                <tk:RadGridView.GridViewGroupPanel>
                                    <tk:GridViewGroupPanel AllowDrop="False" IsEnabled="False" Visibility="Hidden" />
                                </tk:RadGridView.GridViewGroupPanel>
                            </tk:RadGridView>

The CellValidationBehavior logic executes on Attach and Detach:
    using Telerik.Windows.Controls;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CellValidationBehavior : Behavior<RadGridView>
    {
        private EquipmentQuickEntryViewModel _viewModel = null;

        /// <summary>
        /// Overrides the OnAttached event so we can hook up the RowLoaded event
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.DataLoaded += new EventHandler<EventArgs>(DataLoaded);
            this.AssociatedObject.CellValidating += new EventHandler<GridViewCellValidatingEventArgs>(CellValidating);
            this.AssociatedObject.RowValidated += new EventHandler<GridViewRowValidatedEventArgs>(RowValidated);
        }

        /// <summary>
        /// Overrides the OnDetaching method to add handlers to some DragDrop events
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.DataLoaded -= new EventHandler<EventArgs>(DataLoaded);
            this.AssociatedObject.CellValidating -= new EventHandler<GridViewCellValidatingEventArgs>(CellValidating);
            this.AssociatedObject.RowValidated -= new EventHandler<GridViewRowValidatedEventArgs>(RowValidated);
        }

        /// <summary>
        /// Captures the DataLoaded event from the grid
        /// </summary>
        /// <param name="sender">The RadGridView we attached to</param>
        /// <param name="e">The EventArgs</param>
        private void DataLoaded(object sender, EventArgs e)
        {
            var grid = this.AssociatedObject;
            var dataSource = (ObservableCollection<EquipmentToAdd>)grid.ItemsSource;

            if (dataSource.Count>0)
            {
                if (_viewModel == null)
                {
                    _viewModel = grid.DataContext as EquipmentQuickEntryViewModel;
                }

                var selectedRow = _viewModel.SelectedRowNumber;

                grid.ScrollIntoView(grid.Items[selectedRow], grid.Columns["SerialNumber"]);

                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[selectedRow], grid.Columns["SerialNumber"]);

                var equipment = (EquipmentToAdd)grid.Items[selectedRow];

                if (equipment.IsInUse)
                {
                    return;
                }
                
                grid.BeginEdit();
            }
        }

        /// <summary>
        /// Captures the CellValidating event from the grid
        /// </summary>
        /// <param name="sender">The RadGridView we attached to</param>
        /// <param name="e">The GridViewCellValidatingEventArgs</param>
        private void CellValidating(object sender, GridViewCellValidatingEventArgs e)
            {
            if (e.Cell.Column.UniqueName != "SerialNumber")
            {
                return;
            }

            var value = e.NewValue.ToString();

            if (!string.IsNullOrEmpty(value))
            {
                if (this._viewModel == null)
                {
                    this._viewModel = ((RadGridView)sender).DataContext as EquipmentQuickEntryViewModel;
                }
                EquipmentToAdd thisEquipment = (EquipmentToAdd)e.Row.DataContext;
                var isDuplicate = this._viewModel.IsDuplicateSerialNumber(value, thisEquipment);

                if (isDuplicate)
                {
                    e.IsValid = false;
                    e.ErrorMessage = "That serial number already exists";
                    return;
                }

                _viewModel.UpdateCanSave();
            }
        }

        /// <summary>
        /// Captures the RowValidated event from the grid
        /// </summary>
        /// <param name="sender">The RadGridView we attached to</param>
        /// <param name="e">The GridViewRowValidatedEventArgs</param>
        private void RowValidated(object sender, GridViewRowValidatedEventArgs e)
        {
            if (_viewModel == null)
            {
                _viewModel = ((RadGridView)sender).DataContext as EquipmentQuickEntryViewModel;
            }

            _viewModel.UpdateCanSave();
        }
    }

I confirmed that it is being passed the RadGridView object.  However when I enter data into the cell containing the serial number (only editable field) I do not get any validation events.  

WHY??

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 26 Jun 2013, 01:56 PM
Hello,

The CellValidating and RowValidating events will be raised when you commit the edit. Do you do this? You can also check the Validating examples on our WPF Demos.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Michael
Top achievements
Rank 1
answered on 27 Jun 2013, 06:13 PM
The only thing I did not see in our code that was in the example was the parameter: ActionOnLostFocus=CommitEdit

Adding that really does not change behavior.  It is random.
If I populate the grid and all records need serial numbers the on the first field I jump into validation, on the second field I jump into validation, on the third field I don't.  If I mouse around I never get validation.  If I tab around I MIGHT eventually get cell validation.

If I populate the grid and I have a few records turned red and the edittable fields marked readonly, then I get no cell validation at all no matter what I do. 

Cell validation also seemed sensitive to me mousing out of a cell vs. tabbing out which is not acceptable.

Any suggestions?

BTW -- I updated my first post with the current full definition of the RadGridView control settings.  I also updated the cell validation code to encompass all the the code written.  Also this is code I inherited when the guy that wrote it left the company.  So I am going into this cold without any clue as to why he did what he did.  :)
0
Dimitrina
Telerik team
answered on 02 Jul 2013, 02:32 PM
Hi,

The CellValidation event will be raised when the cell is about to CommitEdit, i.e. when you confirm the edit, clicking outside of the cell, or pressing Enter, or pressing the Tab Key.

I get the impression that you would like to validate the data as you enter it into the editor, not as you commit the edit for the GridViewCell. Is that correct?

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Michael
Top achievements
Rank 1
answered on 02 Jul 2013, 02:36 PM
Actually we wanted to validate after the user leaves the cell or hits enter or tabs out.

What we experienced was that the validate event was sporadically being raised by Telerik.  I'd have the validation hit for two fields then stop.  I'd click the entire fracking form and get no validation event then suddenly I'd get one.  

We felt that we could not run around hoping your software would work.  So we changed the design and held back all validation until the save button was clicked.  At least framework elements raise their events when we expect the events to be raise.  So I backed out all of the validation, made the button always enabled, and put all validation on the button.Click event.  Unlike your controls, that always works.
0
Dimitrina
Telerik team
answered on 02 Jul 2013, 02:47 PM
Hello,

I am sorry to hear that you experienced such problems with the validation. Unfortunately I cannot reproduce a problem locally when I leave the cell, hit the Enter key or Tab out of the cell.

If you would like us to further investigate the case, then please open a support thread and send us a repro project, which we can debug locally. That way we can advise you better.


Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Michael
Top achievements
Rank 1
Share this question
or