How to Row Edit End programmatically after Specific Cell Edit Ended

7 posts, 1 answers
  1. NayLin
    NayLin avatar
    5 posts
    Member since:
    Feb 2016

    Posted 20 Apr Link to this post

    I have "Product" AutoCompletebox where the user type Product Name. When the user press enter, this "Product" will be added to the GridView and auto-focus to "Quantity "cell. (I've successfully developed this part.)

    After the user type Quantity in GridView's Cell, I want to Commit and End Edit to this row and focus (cursor) return back to "Product" AutoCompletebox. This is the part that I don't know how to do it.

    Currently I have tried something like the following:

    private void RadGridViewInvoiceItems_CellEditEnded( object sender, GridViewCellEditEndedEventArgs e )
    {
             if ( e.Cell.Column.Header.ToString() == "Quantity")
             {              this.gridView.CommitEdit();
                    this.productAutoCompleteBox.Focus();            
             }
    }

     

    But the above code cause "StackOverFlow" exception. How should I solve this problem?

    P.S. I have developed this app with C# Wpf with MVVM pattern.

    I have "Product" Textbox where the user type Product Name. When the user press enter, this "Product" will be added to the GridView and auto-focus to "Quantity "cell. (I've successfully developed this part.)

    After the user type Quantity in GridView's Cell, I want to CommitEdit to this row and focus return back to "Product" textbox. This is the part that I don't know how to do it.

    Currently I have tried something like the following:

    private void RadGridViewInvoiceItems_CellEditEnded( object sender, GridViewCellEditEndedEventArgs e )
    {
          if ( e.Cell.Column.Header.ToString() == "Quantity" )
          {
               this.gridView.CommitEdit();
               this.productTextBox.Focus();
          }         
    }

    But the above code cause "StackOverFlow" exception. How should I solve this problem?

    P.S. I have developed this app with C# Wpf with MVVM pattern and Telerik controls.

    I have "Product" Textbox where the user type Product Name. When the user press enter, this "Product" will be added to the GridView and auto-focus to "Quantity "cell. (I've successfully developed this part.)

    After the user type Quantity in GridView's Cell, I want to CommitEdit to this row and focus return back to "Product" textbox. This is the part that I don't know how to do it.

    Currently I have tried something like the following:

    private void RadGridViewInvoiceItems_CellEditEnded( object sender, GridViewCellEditEndedEventArgs e )
    {
          if ( e.Cell.Column.Header.ToString() == "Quantity" )
          {
               this.gridView.CommitEdit();
               this.productTextBox.Focus();
          }         
    }

    But the above code cause "StackOverFlow" exception. How should I solve this problem?

    P.S. I have developed this app with C# Wpf with MVVM pattern and Telerik controls.

  2. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 25 Apr Link to this post

    Hello NayLin,

    The reason you're observing the StackOverflowException is that calling the CommitEdit() method raises the GridView's CellEditEnded event, and so an infinite loop is created.

    Could you omit the invocation of the CommitEdit() method and see if the problem persists?

    Note that this will only work when using the Enter key to commit the cell edit. If you want to achieve the same behavior when navigating away with Tab, you will need to define the following custom KeyboardCommandProvider:

    public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
    {
        private GridViewDataControl parentGrid;
        private DefaultKeyboardCommandProvider defaultKeyboardProvider;
        private CustomKeyboardCommandProvider customKeyboardProvider;
        public CustomKeyboardCommandProvider(GridViewDataControl grid)
         : base(grid)
        {
            this.parentGrid = grid;
        }
        public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
        {
            List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
     
            if (key == Key.Tab &&
                this.parentGrid.CurrentColumn.Header.ToString() == "Quantity" &&
                this.parentGrid.CurrentCell.IsInEditMode)
            {
                commandsToExecute.Clear();
                commandsToExecute.Add(RadGridViewCommands.CommitEdit);
            }
     
            return commandsToExecute;
        }
    }

    I hope you find this helpful.

    Regards,
    Dilyan Traykov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. NayLin
    NayLin avatar
    5 posts
    Member since:
    Feb 2016

    Posted 02 May in reply to Dilyan Traykov Link to this post

    Thanks for your answer. I omit the invocation of CommitEdit() method and the StackOverflow exception is gone. But the AutoCompleteBox still hasn't got the Focus.

    I've also tried custom KeyboardCommandProvider without any code errors. But the cell just tab to the another cell. The AutoCompleteBox still hasn't got the Focus.

    My main purpose is to get the Focus on AutoCompleteBox after editing the Quantity cell in the RadGridView. Please look the Attach file for more clear explanation.

  5. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 03 May Link to this post

    Hello NayLin,

    Could you please verify that you've set the GridView's KeyboardCommandProvider similarly to:

    this.RadGridView1.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.RadGridView1);

    I'm attaching a sample project to illustrate the approach I've described. Could you please have a look at it and let me know if I've missed something important?

    Regards,
    Dilyan Traykov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. NayLin
    NayLin avatar
    5 posts
    Member since:
    Feb 2016

    Posted 05 May in reply to Dilyan Traykov Link to this post

    Thanks for you answer and attached sample project. It works perfect.

    Finally I found the problem of why my AutoCompleteBox does not get the focus after CellEditEnded event. I use RadGridView as Edit Template for this Quantity Column. I mean there will be two RadGridViews in my code. Outer RadGridView for my products list and inner RadGridView as Edit Template for the Quantity Column.

    Please look the following code,

    <telerik:RadGridView x:Name="RadGridViewInvoiceItems" Grid.Row="1" ItemsSource="{Binding SaleInvoice.SaleInvoiceItemDecorators}" Margin="0,0,15,0" GroupRenderMode="Flat" RowIndicatorVisibility="Collapsed" IsReadOnly="False" Width="Auto" Height="Auto" ShowColumnSortIndexes="True" AutoGenerateColumns="False" CanUserFreezeColumns="False" CanUserResizeColumns="False" CanUserInsertRows="False" NewRowPosition="Bottom" Loaded="RadGridViewInvoiceItems_Loaded" CellEditEnded="RadGridViewInvoiceItems_CellEditEnded" AddingNewDataItem="RadGridViewInvoiceItems_AddingNewDataItem">
     
           <telerik:RadGridView.Columns>
                       <inf:RowNumberColumn Header="#"
                                Width="30" TextAlignment="Center"
                                CellStyle="{StaticResource RowNumberStyle}" />
     
                       <telerik:GridViewDataColumn Header="Product Name"  DataMemberBinding="{Binding SelectedProduct.ProductName}" Width="*"/>
     
                       <telerik:GridViewDataColumn Header="Supplier Name"  Width="150" DataMemberBinding="{Binding SupplierName}" />
     
                       <telerik:GridViewDataColumn Header="Sale Price" Width="160" TextAlignment="Center" DataMemberBinding="{Binding SalePrice}" />
     
                       <telerik:GridViewDataColumn Header="Quantity" Width="150" DataMemberBinding="{Binding QuantityShowString, Mode=TwoWay}" TextAlignment="Right">
                             <telerik:GridViewDataColumn.CellTemplate>
                                    <DataTemplate>
                                          <TextBlock Text="{Binding QuantityShowString}" FontWeight="Bold" TextAlignment="Center" />
                                    </DataTemplate>
                             </telerik:GridViewDataColumn.CellTemplate>
                             <telerik:GridViewDataColumn.CellEditTemplate>
                                     <DataTemplate>
                                          <telerik:RadGridView AutoGenerateColumns="False"
                                                             ShowGroupPanel="False"
                                                             CanUserReorderColumns="False"
                                                             CanUserSortColumns="False"
                                                             ItemsSource="{Binding Path=ProductUmDecorators}">
                                                    <telerik:RadGridView.Columns>
                                                        <telerik:GridViewDataColumn Header="Qty"  DataMemberBinding="{Binding Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                                        <telerik:GridViewDataColumn Header="Um" DataMemberBinding="{Binding UmShortName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TabStopMode="Skip" />
                                                    </telerik:RadGridView.Columns>
                                                </telerik:RadGridView>
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellEditTemplate>
                        </telerik:GridViewDataColumn>
                        <telerik:GridViewDataColumn Header="Amount"  Width="150" DataMemberBinding="{Binding Amount, Mode=TwoWay}">
                              <telerik:GridViewDataColumn.CellTemplate>
                                     <DataTemplate>
                                         <TextBlock Text="{Binding Amount, StringFormat='\{0:#,#\} (ks)'}" TextAlignment="Right" />
                                     </DataTemplate>
                               </telerik:GridViewDataColumn.CellTemplate>
                       </telerik:GridViewDataColumn>
              </telerik:RadGridView.Columns>
    </telerik:RadGridView>

    I think this "RadGridView" EditTemplate is the main cause of the problem why my AutoCompleteBox does not get the focus. If I remove this "RadGridView" EditTemplate from the code, my AutoCompleteBox get the focus and everything works as expected.

    But as you know, I cannot get the right to remove this "RadGridView" EditTemplate from my code. How should I resolve this problem for my program work as expected.

  7. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 09 May Link to this post

    Hello NayLin,

    A simple workaround I can offer is to set the ActionOnLostFocus property of your parent RadGridView to None. Could you please give that a try and let me know if it works for you?

    Regards,
    Dilyan Traykov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  8. NayLin
    NayLin avatar
    5 posts
    Member since:
    Feb 2016

    Posted 10 May in reply to Dilyan Traykov Link to this post

    Thanks you so much, Dilyan. It finally works. Your support is very nice.
Back to Top
UI for WPF is Visual Studio 2017 Ready