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

[Solved] Cancel modifications in a column with multiple biding within a celledittemplate

3 Answers 131 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joachim
Top achievements
Rank 1
Joachim asked on 05 Oct 2010, 01:33 PM
Hello Telerik Team !

I have a RadGridView like that :

<telerikGridView:RadGridView x:Name="radGridViewSimulations" Grid.Row="1" EditTriggers="None" IsReadOnly="False" ActionOnLostFocus="None" AutoGenerateColumns="False" CanUserReorderColumns="False" DataLoaded="radGridViewSimulations_DataLoaded" BeginningEdit="radGridViewSimulations_BeginningEdit" RowEditEnded="radGridViewSimulations_RowEditEnded">
          <telerikGridView:RadGridView.Columns>
                 <telerikGridView:GridViewDataColumn Header="Ri1" IsReadOnly="False" IsGroupable="False" DataMemberBinding="{Binding Ri1Simu}" >
                        <telerikGridView:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.Column="0" Grid.Row="2">
                                        <TextBlock Text="{Binding Ri1Simu}" TextAlignment="Center" />
                                    </Border>
                                    <Border Grid.Column="0" Grid.Row="3" >
                                        <TextBlock Text="{Binding Ri1Cond}" TextAlignment="Center" />
                                    </Border>
                                    <Border Grid.Column="0" Grid.Row="4" >
                                        <TextBlock Text="{Binding Ri1Prev}" TextAlignment="Center" />
                                    </Border>
                                </Grid>
                            </DataTemplate>
                        </telerikGridView:GridViewDataColumn.CellTemplate>
                        <telerikGridView:GridViewDataColumn.CellEditTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBox Grid.Column="0" Grid.Row="2" Text="{Binding Ri1Simu, Mode=TwoWay}" />
                                    <TextBox Grid.Column="0" Grid.Row="3" Text="{Binding Ri1Cond, Mode=TwoWay}" />
                                    <TextBox Grid.Column="0" Grid.Row="4" Text="{Binding Ri1Prev, Mode=TwoWay}" />
                                </Grid>
                            </DataTemplate>
                        </telerikGridView:GridViewDataColumn.CellEditTemplate>
                    </telerikGridView:GridViewDataColumn>
                           <telerikGridView:GridViewDataColumn IsReadOnly="True" IsGroupable="False" Header="Actions">
                        <telerikGridView:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <telerik:RadButton Grid.Row="1" Grid.Column="0" Click="btnEdit_Click" x:Name="btnEdit" Content="Modifier" />
                                    <telerik:RadButton Grid.Row="1" Grid.Column="0" Click="btnCancel_Click" x:Name="btnCancel" Content="Annuler" Visibility="Collapsed" />
                                    <telerik:RadButton Grid.Row="2" Grid.Column="0" Click="btnSave_Click" x:Name="btnSave" Content="Enregistrer" Visibility="Collapsed" />
                                </Grid>
                            </DataTemplate>
                        </telerikGridView:GridViewDataColumn.CellTemplate>
                    </telerikGridView:GridViewDataColumn>
                     
                     
          </telerikGridView:RadGridView.Columns>
 
</telerikGridView:RadGridView>

And his code behind here :

public partial class SimulationsCustomersPage : Page
    {
 
        /// <summary>
        /// Flag set to true when the page is initialized
        /// </summary>
        private bool _isInitialized = false;
 
        /// <summary>
        /// Data model of this page
        /// </summary>
        private SimulationCustomerListViewModel _model;
 
        /// <summary>
        /// The current grid's row which can be in edit mode
        /// </summary>
        private GridViewRow _currentRow;
 
        /// <summary>
        /// Flag set to true when a gridview's row is in edit mode
        /// </summary>
        private bool _hasRowInEditMode = false;
 
        /// <summary>
        /// The current edit button
        /// </summary>
        private RadButton _currentEditButton;
 
        /// <summary>
        /// The current cancel button
        /// </summary>
        private RadButton _currentCancelButton;
 
        /// <summary>
        /// The current save button
        /// </summary>
        private RadButton _currentSaveButton;
 
        /// <summary>
        /// Flag set to true when the user click on save button
        /// </summary>
        private bool _isValid;
 
        public SimulationsCustomersPage()
        {
            InitializeComponent();
        }
 
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (!_isInitialized)
            {
                radGridViewSimulations.IsBusy = true;
                _model = new SimulationCustomerListViewModel();
 
                Binding binding = new Binding("Simulations");
                binding.Source = _model;
 
                radGridViewSimulations.SetBinding(RadGridView.ItemsSourceProperty, binding);
 
                _isInitialized = true;
            }
 
        }
 
        private void radGridViewSimulations_DataLoaded(object sender, EventArgs e)
        {
            radGridViewSimulations.IsBusy = false;
        }
 
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (!_hasRowInEditMode)
            {
                _hasRowInEditMode = true;
 
                //Hide and show adequate buttons
                _currentEditButton = ((RadButton)sender);
                _currentCancelButton = ((RadButton)sender).ParentOfType<Grid>().FindName("btnCancel") as RadButton;
                _currentSaveButton = ((RadButton)sender).ParentOfType<Grid>().FindName("btnSave") as RadButton;
 
                //Sets the 'Ri1' cell in edit mode
                _currentRow = (sender as UIElement).ParentOfType<GridViewRow>();
                radGridViewSimulations.CurrentItem = _currentRow.Item;
                var cell = _currentRow.GetCellFromPropertyName("Ri1Simu");
                cell.IsCurrent = true;
                radGridViewSimulations.BeginEdit();
            }
            else
            {
                RadWindow.Alert(new DialogParameters()
                {
                    Content = "Vous ne pouvez modifier qu'une ligne à la fois.\nVeuillez annuler ou valider vos modifications courantes pour continuer",
                    Header = "Action non autorisée"
                });
            }
        }
 
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            radGridViewSimulations.CancelEdit();
        }
 
        /// <summary>
        /// Show save/cancel buttons and hide edit button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radGridViewSimulations_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
        {
            if (_hasRowInEditMode)
            {
                //We authorize temporarly to use a click on a cell to enter in edit mode
                radGridViewSimulations.EditTriggers = GridViewEditTriggers.CellClick;
 
                _currentEditButton.Visibility = System.Windows.Visibility.Collapsed;
                _currentCancelButton.Visibility = _currentSaveButton.Visibility = System.Windows.Visibility.Visible;
            }
        }
 
        /// <summary>
        /// Save the user's modification and exit the edit mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //the grid is valid now !
            _isValid = true;
 
            radGridViewSimulations.CommitRowEdit(_currentRow);
        }
 
        private void radGridViewSimulations_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            _hasRowInEditMode = false;
            _isValid = false;
 
            _currentSaveButton.Visibility = _currentCancelButton.Visibility = System.Windows.Visibility.Collapsed;
            _currentEditButton.Visibility = System.Windows.Visibility.Visible;
 
            //The use of a click to enter in edit mode is forbidden
            radGridViewSimulations.EditTriggers = GridViewEditTriggers.None;
 
            if (e.EditOperationType == GridViewEditOperationType.Edit)
            {
                //Update the data base !
                 
            }
        }
    }

Finaly my model is :

public partial class Simulation : INotifyPropertyChanged
 
{
 
    #region INotifyPropertieChanged Members
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
 
    #endregion
 
    public virtual decimal Ri1Simu
    {
 
        get { return _ri1Simu; }
        set
        {
            if (_ri1Simu != value)
            {
                _ri1Simu = value;
                OnPropertyChanged("Ri1Simu");
            }
        }
    }
    private decimal _ri1Simu;
     
    public virtual decimal Ri1Cond
    {
 
        get { return _ri1Cond; }
        set
        {
            if (_ri1Cond != value)
            {
                _ri1Cond = value;
                OnPropertyChanged("Ri1Cond");
            }
        }
    }
    private decimal _ri1Cond;
     
    public virtual decimal Ri1Prev
    {
 
        get { return _ri1Prev; }
        set
        {
            if (_ri1Prev != value)
            {
                _ri1Prev = value;
                OnPropertyChanged("Ri1Prev");
            }
        }
    }
    private decimal _ri1Prev;
}

Now my problem is :
When the user cancel his modifications only the modifications on "Ri1Simu"'s property are cancelled and "Ri1Cond", "Ri1Prev" properties are not :(
How can i achieve this ?

Thanks for your help guys !

Joachim.

3 Answers, 1 is accepted

Sort by
0
Joachim
Top achievements
Rank 1
answered on 06 Oct 2010, 04:53 PM
Please i need some help...

What is the best way to bind a cell with multiple properties ?
i search a way to do multiple binding in a column and be able to use the native telerik validation, and edit mode ?

I tried to create a custom column editor too but i did not managed to create a property with the same behavior as DataMemberBinding property...
Can you please tell me if what i want it's possible ?
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 07 Oct 2010, 03:16 PM
Hello JOACHIM,

This is the right way, but using CellTemplate and CellEditTemplate properties will by-pass RadGridView's editing and validation logic. So you cannot count to RadGridView to properly update underlying properties.

By the way I cannot simulate how only one property is updated while other two are not updated.
Could you send me some more information about your scenario in order to find a proper solution?

One idea is to wrap all editing elements into a user control which will expose property which will be bound via Column.DataMemberBinding property.

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
0
Joachim
Top achievements
Rank 1
answered on 07 Oct 2010, 03:25 PM
Thank you for your response Nedyalko,

I understand now that i cannot count to RadGridView to properly update underlying properties if i use cell edit template.
Thank you for the idea, i think i will follow your advice.

By the way there's no problem when updating the three datas together.

Ciao,

Joachim.
Tags
GridView
Asked by
Joachim
Top achievements
Rank 1
Answers by
Joachim
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or