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

Problems when adding new rows and validating

3 Answers 272 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 01 Nov 2010, 04:32 PM
Hello,

I am trying to use RadGridView in one project but I have been experiencing some difficulties

Basically I have a GridView with ItemSource binding to my list of items.

Problem is data, when I try to add a new row to the grid, using BeginInsert method,
instead of start editing the new row, the grid starts editing a blank cell on the previous row (this cell can be left blank)

Also sometimes when a row enters in editing mode, doesn't matter what I do (press insert key, etc..)
it stays this way...

P.S: Just to notice that  I am using MVVM, so all the events are made by use of Commands

Thanks a lot

3 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 02 Nov 2010, 10:15 AM
Hi Igor,

Could you please send me some more information about your scenario (sample code and xaml)?

Sincerely yours,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Igor
Top achievements
Rank 1
answered on 02 Nov 2010, 11:41 AM
Hi Nedyalko,

this is a little bit of my code:

In the XAML:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
<telerik:RadButton Width="150" Content="Delete selected rows" Margin="0,0,5,0" 
   Command="{Binding DeleteSelectedItemsCommand}" IsEnabled="{Binding CanDeleteFields}"/>
        <telerik:RadButton Width="150" Content="Add new row" Margin="0,0,5,0"
                           Command="telerik:RadGridViewCommands.BeginInsert" CommandTarget="{Binding ElementName=RadGridViewUnits}" />
</StackPanel>
<telerik:RadGridView x:Name="RadGridViewUnits" 
 Grid.Row="1"
 ItemsSource="{Binding Items}"
 AutoGenerateColumns="False"
 SelectionMode="Extended">
<i:Interaction.Triggers>
         <i:EventTrigger EventName="AddingNewDataItem" >
                 <cmd:EventToCommand 
Command="{Binding AddNewItemCommand}"
PassEventArgsToCommand="True"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="SelectionChanged" >
                 <cmd:EventToCommand 
Command="{Binding SelectionChangedCommand}"
PassEventArgsToCommand="True"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="CellValidating" >
<cmd:EventToCommand 
Command="{Binding CellValidatingCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="RowValidating" >
<cmd:EventToCommand 
Command="{Binding RowValidatingCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<telerik:RadGridView.Columns>
         <telerik:GridViewDataColumn Header="Code" DataMemberBinding="{Binding Code}"/>
<telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Description}"/>
<telerik:GridViewDataColumn Header="Is Integer" DataMemberBinding="{Binding IsInteger}"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>

In the viewmodel:
	/// <summary>
        /// Updates selected items in Grid.
        /// </summary>
        private void SelectionChanged(SelectionChangeEventArgs e)
        {
            foreach (var addedItem in e.AddedItems)
            {
                SelectedItems.Add((UnitsOfMeasure)addedItem);
            }
            foreach (var removedItem in e.RemovedItems)
            {
                SelectedItems.Remove((UnitsOfMeasure)removedItem);
            }
            NotifyPropertyChanged(() => CanDeleteFields);
        }

        /// <summary>
        /// Validates a Property submitted in a cell of the Grid.
        /// </summary>
        private void CellValidating(GridViewCellValidatingEventArgs e)
        {
            if (e.Cell.Column.UniqueName == "Code")
            {
                foreach (UnitsOfMeasure item in Items.Where(item => 
                        (item.Code == e.NewValue.ToString() && item != (UnitsOfMeasure)e.Row.Item)))
                {
                    e.IsValid = false;
                    e.ErrorMessage = "You cannot have two Units of Measure with the same Code.";
                }
                if (e.NewValue.ToString().Length > 12)
                {
                    e.IsValid = false;
                    e.ErrorMessage = "Code shouldn't be longer than 12 characters.";
                }
                else if (e.NewValue.ToString().Length == 0)
                {
                    e.IsValid = false;
                    e.ErrorMessage = "Code shouldn't be empty.";
                }
            }
        }


        /// <summary>
        /// Validates a Item submitted in a row of the Grid.
        /// </summary>
        private void RowValidating(GridViewRowValidatingEventArgs e)
        {
            if(((UnitsOfMeasure)e.Row.DataContext).Code == null)
            {
                e.IsValid = false;
            }
        }

        /// <summary>
        /// Adds a field to the SelectedItems List.
        /// </summary>
        private void AddNewItem(GridViewAddingNewEventArgs e)
        {
            if (Items.Count == 0 || Items[Items.Count - 1].Code != null)
            {
                UnitsOfMeasure unit = new UnitsOfMeasure {Company = _currentCompany};
                e.NewObject = unit;
            }
            else
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Delete the items in the SelectedItems List.
        /// </summary>
        private void DeleteSelectedItems()
        {
            List<UnitsOfMeasure> deletedItems = new List<UnitsOfMeasure>(SelectedItems);
            foreach (UnitsOfMeasure item in deletedItems)
            {
                Items.Remove(item);
            }
            NotifyPropertyChanged(() => Items);
        }


Problems are:
  • When "Add a new row" is pressed for the first time, it creates the new row and starts editing the first cell
  • When this is done again, instead of editing the new row, it starts editing the last one
  • Also after this, the previous row that enter in editing mode doesn't go back to view mode when loses focus or "enter" key is pressed, and the cellvalidating event also doesn't fire

Thanks again

0
Nedyalko Nikolov
Telerik team
answered on 04 Nov 2010, 10:58 AM
Hello Igor,

I've tried to compile your code unfortunately to no avail. Could you please open a separate support ticket and attach a project which I can debug on my side?
Thank you in advance.

Kind regards,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Igor
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Igor
Top achievements
Rank 1
Share this question
or