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

Text of RadMaskedTextInput not shown

1 Answer 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vadimir
Top achievements
Rank 1
Vadimir asked on 01 Oct 2013, 02:43 PM
Hi,
I'm developing with VS 2012 and Telerik control version 2013.2.724.40.
I tried to bind string values dynamically to the GridView in code.
The program first reads from a array, where it can decide the type of the column(e.g. string, double). If the data can be presented as a double value, then I create a customized GridView column with RadMaskedTextInput. I'd like to format the number by setting the Text property. The data binding works fine, however, it seems the numbers (e.g. 1000.1) are not shown as expected (e.g. 1,000.10). 

I could attach all source code if needed. Thanks in advance.

Regards,
Gong

public MainWindow()
{
    InitializeComponent();
 
    IList<DataHeader> headerList = DataService.Instance.HeaderList;
 
    for (int i = 0; i < headerList.Count; i++)
    {
        DataHeader curentHeader = headerList[i];
 
        GridViewBoundColumnBase column;
        if (curentHeader.Name == "AMOUNT")
        {
            column = new CGridViewNumColumn();
        }
        else
        {
            column = new GridViewDataColumn();
        }
 
        column.DataMemberBinding = new Binding("DataItemList[" + i + "]")
        {
            UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
        };
        column.Header = curentHeader.Caption;
        column.UniqueName = curentHeader.Name;
        GridControl.Columns.Add(column);
    }
    DataContext = DataService.Instance.Data;
}


internal class CGridViewNumColumn : GridViewBoundColumnBase
    {
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            BindingTarget = RadMaskedTextInput.ValueProperty;
            var input = new RadMaskedTextInput
            {
                HorizontalContentAlignment = HorizontalAlignment.Right,
                Culture = CultureInfo.GetCultureInfo("en-GB"),
                FormatString = "n2",
                Mask = "",
                MinWidth = 100,
                SelectionOnFocus = SelectionOnFocus.SelectAll
            };
            input.SetBinding(BindingTarget, CreateValueBinding());
            input.LostFocus += InputOnLostFocus;
            return input;
        }
 
        private void InputOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
        {
            var input = sender as RadMaskedTextInput;
            if (input == null) throw new ArgumentNullException("sender");
            if (string.IsNullOrWhiteSpace(input.Value))
            {
                input.Value = string.Empty;
                return;
            }
            double value;
            //Format the number for frontend.
            const NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands;
            if (!double.TryParse(input.Value, style, input.Culture, out value))
            {
                input.Value = "0";
            }
            input.Text = value.ToString(input.FormatString, input.Culture);
 
            //Format the number for back-end. Extra precisions will be removed.
            if (!double.TryParse(input.Text, style, input.Culture, out value))
            {
                input.Value = "0";
            }
            input.Value = value.ToString(input.Culture);
        }
 
        private Binding CreateValueBinding()
        {
            var valueBinding = new Binding
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true,
                Path = new PropertyPath(DataMemberBinding.Path.Path)
            };
 
            return valueBinding;
        }
    }

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 02 Oct 2013, 07:35 AM
Hi,

I have checked your solution and I can see that the RadMaskedTextInput you have placed in edit mode for one of the GridView's columns does not format as you would like.

I placed a RadMaskedTextInput outside RadGridView and the format was also not respected. Please note that RadGridView does not have any impact on the formatting to be applied on the element placed in edit mode. In that case, it is the editor that is responsible for its formatting.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Vadimir
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or