We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adding new row, set values based on previous column value

On my data bound GridGroupingControl I want users to enter a new invoice. The user selects a payee, enters invoice date and invoice amount. Only when I have these 3 pieces of information I am able to determine the split for each party.

I trigger the "TableControlCurrentCellValidating" each time I select/enter a value and move to the next cell to extract the values entered for Payee, Invoice Date and Invoice Amount and determine if I have all the values required. Once all these values have been collected, I calculate the amount for each party and populate the proper columns in this new record with these values.

The issue I am facing is that the new record is cancelled (when using record.EndEdit(); as mentioned in some examples) or doesn't move to the next cell (as in code below) when pressing TAB.

Question, how do I correctly populate the calculated values in the columns of an AddNewRecord, prevent the TableControlCurrentCellValidating from firing twice, and have the TAB key (which triggered the TableControlCurrentCellValidating event) on moving to the next cell (or previous in case of SHIFT TAB) once data is set for the current new record row.

Thank you.

-----------------------------------------------------------------------------
private void GridGroupingInvoice_TableControlCurrentCellValidating(object sender, GridTableControlCancelEventArgs e)
        {
            var currentCell = e.TableControl.CurrentCell;
            var style = e.TableControl.Model[currentCell.RowIndex, currentCell.ColIndex] as GridTableCellStyleInfo;

            // Cancel processing id not Add New Record.
            if ((style.TableCellIdentity.Column == null) || (style.TableCellIdentity.TableCellType != GridTableCellType.AddNewRecordFieldCell))
            {
                return;
            }

            if (style.TableCellIdentity.Column.MappingName == "PayeeId")
            {
                if (CurrentInvoiceAllocation == null)
                {
                    CurrentInvoiceAllocation = new InvoiceAllocation();
                }
                CurrentInvoiceAllocation.PayeeId = int.Parse(currentCell.Renderer.ControlValue.ToString());
            }

            if (style.TableCellIdentity.Column.MappingName == "InvoiceDate")
            {
                if (CurrentInvoiceAllocation == null)
                {
                    CurrentInvoiceAllocation = new InvoiceAllocation();
                }
                CurrentInvoiceAllocation.InvoiceDate = DateTime.Parse(currentCell.Renderer.ControlValue.ToString());
            }

            if (style.TableCellIdentity.Column.MappingName == "InvoiceAmount")
            {
                if (CurrentInvoiceAllocation == null)
                {
                    CurrentInvoiceAllocation = new InvoiceAllocation();
                }
                CurrentInvoiceAllocation.InvoiceAmount = decimal.Parse(currentCell.Renderer.ControlValue.ToString());
            }

            // Do not calculate for columns other than PayeeId, InvoiceDate and InvoiceAmount.
            if ((style.TableCellIdentity.Column.MappingName != "PayeeId") && (style.TableCellIdentity.Column.MappingName != "InvoiceDate") && (style.TableCellIdentity.Column.MappingName != "InvoiceAmount"))
            {
                return;
            }

            // Check if all information exists for proper calculation
            if (!CurrentInvoiceAllocation.Calculate())
            {
                CenteredMessageBox.Notify(this, "Could not calculate the allocation per party for this payee.");
                return;
            }

            // Calculate the allocation amounts for this invoice.
            var record = e.TableControl.Table.AddNewRecord;
            if (record != null)
            {
                e.TableControl.BeginUpdate();
                currentCell.IsModified = false;

                record.SetValue("Amount1", CurrentInvoiceAllocation.Amount1);
                record.SetValue("Amount2", CurrentInvoiceAllocation.Amount2);
                record.SetValue("Amount3", CurrentInvoiceAllocation.Amount3);

                e.TableControl.EndUpdate(true);

                e.TableControl.RefreshRange(GridRangeInfo.Row(currentCell.RowIndex));

                // Move to the next cell.
                //e.TableControl.CurrentCell.InternalMove(GridDirectionType.Right, 0, GridSetCurrentCellOptions.ScrollInView);
            }
        }


1 Reply

MG Mohanraj Gunasekaran Syncfusion Team December 15, 2016 01:06 PM UTC

Hi Patrick, 
Thanks for using Syncfusion products. 
After analyzation of your code part, you have called the Calculate method for every cell validation so please call that method only for Amount columns when differ the entered amount value and calculated amount value in TableControlCurrentCellValidating event. Please refer the below code snippet and refer the attached sample, 

Code snippet: 
if (style.TableCellIdentity.Column.MappingName == "InvoiceAmount") 
{ 
    if (CurrentInvoiceAllocation == null) 
    { 
        CurrentInvoiceAllocation = new InvoiceAllocation(); 
    } 
    decimal invoiceAmount = decimal.Parse(currentCell.Renderer.ControlValue.ToString()); 
    if (CurrentInvoiceAllocation.InvoiceAmount != invoiceAmount) 
    { 
        CurrentInvoiceAllocation.InvoiceAmount = invoiceAmount; 
        // Check if all information exists for proper calculation 
        if (!CurrentInvoiceAllocation.Calculate()) 
        { 
            MessageBox.Show("Could not calculate the allocation per party for this payee."); 
            return; 
        } 
    } 
} 

If we misunderstood your query, please provide the screenshot that shows your scenario or modify the below attached sample and revert us. It will be helpful to provide the solution at the earliest. 

Sample link: GridGroupingControl 

Regards, 
Mohanraj G. 


Loader.
Live Chat Icon For mobile
Up arrow icon