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

Editing a decimal value in a Numerical Column

4 Answers 179 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 23 Mar 2018, 12:13 PM

I am trying to edit a decimal value in a DataGridNumericalColumn, but the value does not get set when the changes are committed. Values are displayed correctly and the value is shown in the RadNumericBox for editing, but the changes are not applied to the datasource value. All other columns work correctly.

I have a CommitEdit event used for additional processing and the updated value does not reach this event.

Am I does something wrong here?

<tg:DataGridNumericalColumn PropertyName="Payment" Header="Amount"
                            CellContentStyle="{StaticResource DefaultGridNumericStyle}"
                            CellContentFormat="{}{0,0:C2}"
                            SizeMode="Auto"
                            tcp:CultureService.CultureName="en-GB">
    <tg:DataGridNumericalColumn.CellEditorStyle>
        <Style TargetType="tci:RadNumericBox">
            <Setter Property="Margin" Value="5,5,5,0"/>
            <Setter Property="Maximum" Value="9999999999"/>
            <Setter Property="ValueFormat" Value="{}{0,0:C2}"/>
            <Setter Property="AllowNullValue" Value="False"/>
            <Setter Property="ButtonsVisibility" Value="Visible"/>
        </Style>
    </tg:DataGridNumericalColumn.CellEditorStyle>
</tg:DataGridNumericalColumn>

 

My CommitEdit event:

private void OnPaymentCommitEdit(EditContext parameter)
{
    if (parameter != null && parameter is EditContext)
    {
        EditContext context = (EditContext)parameter;
 
        if (context.CellInfo.Item is Payments)
        {
            // at this point - if I examine the decimal value that has been changed, the change has not been applied to the item
 
 
            ViewModel.RefreshPaymentLookupValues((Payments)context.CellInfo.Item);
        }
    }
 
    ViewModel.PaymentChangesMade = true;
    ViewModel.IsPaymentBeingEdited = false;
}

4 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 23 Mar 2018, 03:06 PM

After further delving into the documentation, I see that the radNumericBox only supports the type of double.

So how do I use a standard Text column to edit a value in a RadDataGrid that is a decimal currency value? For normal editing in a TextBox control I use a DecimalToCurrencyStringConverter when getting/setting the value, but I can't see how to do this.

I tried to use a TemplateColumn, with a DataTemplateSelector, switching from TextBlock to TextBox on BeginEdit. But I got lost trying to bind the value of the property to the TextBox using the Converter.

The worst case solution would be to change the precision of the field in the database so I can use double, but this seems a compromise too far, as it might be dangerous when the field contains possibly high currency values (at the moment I use the money type on a SQL Server table).

0
David
Top achievements
Rank 1
answered on 26 Mar 2018, 08:50 AM

I found an acceptable compromise that didn't require a change to the database.

I access the database via a data service, and the code I use to read the data is happy to read the value of the decimal field into a double. So I removed the money (decimal) field from the partial class in my data model and re-created it as a double in a local partial class for the entity. There is a risk, admittedly extremely small, that the value is too great for the double and so is truncated or rounded, but the risk is acceptable. 

0
Accepted
Stefan Nenchev
Telerik team
answered on 26 Mar 2018, 02:36 PM
Hi, David,

I am glad to see that you have managed to find a solution. I am here providing the suggestion which I have included in the support ticket you have created so that other customers might have it in mind as well.

Regarding the initial approach you have taken, in the DataTemplate you are using when in edit mode, you can simply put a TextBox and use the following way of binding(where GDP is the decimal property on your object):

<DataTemplate>
    <TextBox Text="{Binding GDP}"
             Foreground="Green"/>
</DataTemplate>

Note that you can use a Converter of your choice by directly setting it on the Binding within the Text property.

A slightly different approach you can try is using a "proxy" property. So for example, you can create a GDPproxy property which will be a string representation of the decimal value. You can use it in combination with a DataGridTextColumn and when you set its value - to parse the value to decimal and set your decimal property as well. 

Have a great rest of the week.

Regards,
Stefan Nenchev
Progress Telerik
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 Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 26 Mar 2018, 02:59 PM
Thanks
Tags
DataGrid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or