How to Row Edit End programmatically after Specific Cell Edit Ended

1 Answer 6885 Views
GridView
NayLin
Top achievements
Rank 1
NayLin asked on 20 Apr 2016, 04:19 PM

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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 25 Apr 2016, 01:08 PM
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.
NayLin
Top achievements
Rank 1
commented on 02 May 2016, 08:54 AM

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.

Dilyan Traykov
Telerik team
commented on 03 May 2016, 08:59 AM

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.
NayLin
Top achievements
Rank 1
commented on 05 May 2016, 11:25 AM

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.

Dilyan Traykov
Telerik team
commented on 09 May 2016, 03:57 PM

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.
NayLin
Top achievements
Rank 1
commented on 10 May 2016, 06:39 AM

Thanks you so much, Dilyan. It finally works. Your support is very nice.
Hüseyin
Top achievements
Rank 1
commented on 22 Oct 2018, 07:42 PM

Hi Dilyan, i have a problem.

When i refresh datasource of my gridview, My gridview enter infinite loop.

When i refresh datasource of my gridview, Always enter celleditenden event :(

But i dont want this. I want when i refresh my datasource, refresh gridview and not infinite loop(celleditended).

private void saleslineRadGridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
        {
            try
            {
                //var orcacolumn = e.EditingElement as OrcaColumnTextboxUserControl;
               var controlsalesline = saleslineRadGridView.SelectedItem as SalesLine;
                SalesLines saleslinelist = new SalesLines();
                saleslinelist.Reset();
                saleslinelist.SETRANGE("DocumentNo", _SalesHeader.DocumentNo);
                saleslinelist.FIND();
                var aretheresalesline = saleslinelist.tableList.Where(x => x.UniqueID == controlsalesline.UniqueID).FirstOrDefault();

                if (!String.IsNullOrWhiteSpace(controlsalesline.DocumentNo)
                    && !String.IsNullOrWhiteSpace(controlsalesline.LineNo.ToString())
                    && aretheresalesline != null)
                {
                    if (e.Cell.DataColumn.UniqueName == "ItemNo")
                    {
                        var selectedsalesline = saleslineRadGridView.SelectedItem as SalesLine;
                        if (selectedsalesline != null)
                        {
                            Items Itemlist = new Items();
                            Itemlist.Reset();
                            Itemlist.SETRANGE("No", controlsalesline.ItemNo);
                            Itemlist.FIND();
                            if (Itemlist.tableList.Count > 0 && selectedsalesline.ItemNo != e.OldData.ToString())
                            {
                                SQLSelectQueryCreator queryCreator = new SQLSelectQueryCreator();
                                queryCreator.TRAN();
                                if (queryCreator.RunStoredProcedure("sp_ModifySalesLine", _SalesHeader, selectedsalesline, null, null, null, null, null))
                                {
                                    queryCreator.COMMIT();
                                    RefreshSalesLineGridView();  /////////// LATER INFINITE LOOP :(  //////////////////////
                                }
                            }

Dilyan Traykov
Telerik team
commented on 24 Oct 2018, 12:05 PM

Hello Hüseyin,

I'm afraid that the provided code is insufficient for me to suggest a concrete cause of the issue you observe.

Could you please place a breakpoint inside the handler and share the stack trace after the first loop iteration so that we can pinpoint why the infinite loop occurs? Ideally, please open a new support ticket and send over your current approach isolated in a small sample project so that I can investigate at my end.

I look forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 06 Dec 2019, 06:05 AM

hii..How to add new row programmatically after specific Cell Edit Ended

Regards,

Ranees



Dilyan Traykov
Telerik team
commented on 10 Dec 2019, 10:18 AM

Hi Ranees,

Is there a particular reason the approach I suggested earlier would not work for you? If that is the case, please provide more details about your setup and exact requirement so that I can better assist you.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 21 Dec 2019, 06:39 AM

hiii  Dilyan Traykov...i had tried to add new row by using  AddingNewDataItem event...but this event not firing in my case

regards,

Ranees

here is my code....
-------------------------
 
 <telerik:RadGridView CanUserInsertRows="True"   ActionOnLostFocus="None"  ShowColumnFooters="True"  Style="{StaticResource MyGridStyle}"  BorderThickness="0,0,0,0" GridLinesVisibility="Both" GroupRenderMode="Flat" LeftFrozenColumnCount="0" x:Name="dgvItems2" ScrollViewer.PanningMode="Both"  RowIndicatorVisibility="Collapsed" 
VerticalAlignment="Top" Height="353" SelectionUnit="Cell" Background="#FFF9F9F9"
HorizontalAlignment="Left" Width="1200" Margin="-9,1,0,0" AlternateRowBackground="White" Grid.Row="1"  AddingNewDataItem="dgvItems2_AddingNewDataItem"   / >

 

Dinko | Tech Support Engineer
Telerik team
commented on 25 Dec 2019, 11:06 AM

Hello ranees,

Thank you for the provided code snippet.

Looking at the code snippet the NewRowPosition property is not set to the RadGridView. To be able to add new row runtime you need to set this property to Top or Bottom. Can you set it on your side and let me know if the AddingNewDataItem event is called on your side.

As a side note, we prefer to keep separate questions in separate threads because it is easier to follow a conversation - easier for us to answer your questions and easier for you to find our answers. I will ask that you open a new forum thread for the new questions you have as they are not related to the original one, which this thread started. Thank you for understanding.

Regards,
Dinko
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 26 Dec 2019, 04:29 AM

thank you Dinko for the reply..but i am using  RadGridView for sales invoice i want to add an empty row in window load and specific CellEdit change ...how does it  work in that case ? would you please show me some example 

regards,
Ranees

 

Tags
GridView
Asked by
NayLin
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or