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
close icon

edit one column update the value in another column

Say I have 2 columns in the grid, Debit and Credit, edit one should automatically update the other column BEFORE saving.

I thought it should be simple, but couldn't find a way to handle that.

1. I tried CellSave and CellEdit in GridEvent, seems it only works in Batch  mode.  I prefer inline mode.

2. I tried to use EditTemplate in the ColumnCell, use a NumericTextBox with a ValueChange event handler, it can catch the event, but through the argument I doesn't have a way to change the row data.

Can you guys provide me some answer here?



3 Replies

VN Vignesh Natarajan Syncfusion Team February 5, 2020 05:54 PM UTC

Haizhi,  
 
Greetings from Syncfusion support.  
 
Query: “Debit and Credit, edit one should automatically update the other column BEFORE saving 
 
From your query we understand that you want to change one column value based on the other and want to use NumericTextBox component in EditForm. You can achieve this requirement using EditTemplate feature of EjsGrid. We have used ValueChange event of NumericText box control to change the value of another column using property binding.  
 
Since you want to change the value one column while changing the other column, we have used the flag (SU,DU) variable to achieve your requirement. And also we have used Boolean variable to identify whether the record is being editing to ensure that correct value is displayed in Grid edit form. 
   
Refer the below code example. 
 
<EjsGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Edit""Delete""Cancel""Update" })" Height="315"> 
    <GridEvents OnActionBegin="Begin" TValue="Order"></GridEvents> 
. . . . . . . . . . . 
        <GridColumn Field=@nameof(Order.Credit) HeaderText="Credit" Format="C2" TextAlign="TextAlign.Right" Width="120"> 
            <EditTemplate> 
                @{ 
                    var order = (context as Order); 
                    <EjsNumericTextBox ID="Credit" TValue="int?" Value=@(GetValue(order,"Credit"))> 
                        <NumericTextBoxEvents ValueChange="@((args)=>OnCreditChange(args,order))" TValue="int?"></NumericTextBoxEvents> 
                    </EjsNumericTextBox> 
                } 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.Debit) HeaderText="Debit" Format="C2" TextAlign="TextAlign.Right" Width="120"> 
            <EditTemplate> 
                @{ 
                    var order = (context as Order); 
                    <EjsNumericTextBox ID="Debit" TValue="int?" Value=@(GetValue(order,"Debit"))> 
                        <NumericTextBoxEvents ValueChange="@((args)=>OnDebitChange(args,order))" TValue="int?"></NumericTextBoxEvents> 
                    </EjsNumericTextBox> 
                } 
            </EditTemplate> 
        </GridColumn> 
    </GridColumns> 
</EjsGrid> 
  
@code{ 
    public intGetValue(Order orderstring columns) 
    { 
        if (columns == "Credit") 
        { 
            if (isEdit) 
                CreditScore = null; 
            return ((int?)(CreditScore ?? order.Credit)); 
        } 
        else 
        { 
            if (isEdit) 
                DebitScore = null; 
            return ((int?)(DebitScore ?? order.Debit)); 
        } 
    } 
    public bool CU = false; 
    public bool DU = false; 
    public bool isEdit = false; 
    public int? DebitScore { getset; } = null; 
    public int? CreditScore { getset; } = null; 
    public List<Order> Orders { getset; } 
    public void Begin(ActionEventArgs<OrderArgs) 
    { 
        if (Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.BeginEdit) 
        { 
            isEdit = true; 
        } 
    } 
    public void OnCreditChange(Syncfusion.EJ2.Blazor.Inputs.ChangeEventArgs ArgsOrder Value) 
    { 
        isEdit = false; 
        if (!CU) 
        { 
            DebitScore = Value.principal - Convert.ToInt32(Args.Value); 
            DU = true; 
        } 
        else 
        { 
            CU = false; 
        } 
    } 
    public void OnDebitChange(Syncfusion.EJ2.Blazor.Inputs.ChangeEventArgs ArgsOrder Value) 
    { 
        isEdit = false; 
        if (!DU) 
        { 
            CreditScore = Value.principal - Convert.ToInt32(Args.Value); 
            CU = true; 
        } 
        else 
        { 
            DU = false; 
        } 
    }    
} 
 
 
Note: we have used two Boolean flag variable, since we are updating the other column value in change event and vice versa. Other wise it might lead to change the value continuously between them upon changing.  
 
For your convenience we have prepared a sample which can be downloaded from below  
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 
 



HZ haizhi zhong February 5, 2020 11:27 PM UTC

Perfect, it works!

And thank you for providing the sample code, that helps a lot. 


VN Vignesh Natarajan Syncfusion Team February 6, 2020 03:59 AM UTC

Hi Haizhi,  

Thanks for the update.  

We are glad to hear that you have resolved your query using our solution.  

Kindly get back to us if you need any further assistance from us.  

Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon