Completed
Last Updated: 06 Jul 2018 11:24 by ADMIN
ADMIN
Hristo
Created on: 29 Jun 2018 10:03
Category: GridView
Type: Bug Report
1
FIX. RadGridView - the RadMaskedEditBoxEditor should respect null values if the EnableNullValueInput is set to true
How to reproduce: check the attached project and comment the workaround

Workaround: 
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    private BindingList<TestDataObject> data;
    public RadForm1()
    {
        InitializeComponent();

        this.data = new BindingList<TestDataObject>();
        this.LoadData();

        GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn();
        textColumn.Name = "Name";
        textColumn.FieldName = "Name";
        textColumn.HeaderText = "Name";
        this.radGridView1.Columns.Add(textColumn);

        GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn();
        dateColumn.Name = "Date";
        dateColumn.FieldName = "Date";
        dateColumn.HeaderText = "Date";
        this.radGridView1.Columns.Add(dateColumn);

        GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn();
        maskBoxColumn.Name = "Price";
        maskBoxColumn.FieldName = "Price";
        maskBoxColumn.HeaderText = "Price";
        maskBoxColumn.MaskType = MaskType.Numeric;
        maskBoxColumn.Mask = "C";
        maskBoxColumn.TextAlignment = ContentAlignment.BottomRight;
        maskBoxColumn.FormatString = "{0:C}";
        maskBoxColumn.DataType = typeof(decimal);
        this.radGridView1.Columns.Add(maskBoxColumn);

        this.radGridView1.DataSource = this.data;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

        //Workaround
        this.radGridView1.CellBeginEdit += RadGridView1_CellBeginEdit;
        this.radGridView1.EditorRequired += RadGridView1_EditorRequired;
    }

    void RadGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (e.EditorType == typeof(RadMaskedEditBoxEditor))
        {
            e.EditorType = typeof(MyRadMaskedEditBoxEditor);
        }
    }

    private void RadGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        RadMaskedEditBoxEditor maskedEditBoxEditor = e.ActiveEditor as RadMaskedEditBoxEditor;
        if (maskedEditBoxEditor != null && e.Row.Cells[e.ColumnIndex].Value == null)
        {
            maskedEditBoxEditor.MaskTextBox.EnableNullValueInput = true;
        }
        else
        {
            maskedEditBoxEditor.MaskTextBox.EnableNullValueInput = false;
        }
    }

    private void LoadData()
    {
        for (int i = 0; i < 1000; i++)
        {
            decimal? price = null;
            if (i % 2 == 0)
            {
                price = i * 100;
            }

            this.data.Add(new TestDataObject
            {
                Name = "Name " + i,
                Date = DateTime.Now.AddDays(i),
                Price = price
            });
        }
    }
}

public class TestDataObject
{
    public string Name { get; set; }

    public DateTime Date { get; set; }

    public decimal? Price { get; set; }
}

public class MyRadMaskedEditBoxEditor : RadMaskedEditBoxEditor
{
    public override object Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            if (value == null && (this.MaskTextBox.EnableNullValueInput || this.MaskTextBox.MaskType != MaskType.Numeric))
            {
                this.MaskTextBox.Value = this.NullValue;
            }
            else
            {
                base.Value = value;
            }
        }
    }
}
1 comment
ADMIN
Dimitar
Posted on: 06 Jul 2018 11:24
New property in the GridViewMaskBoxColumn (EnableNullValueInput):

GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn();
maskBoxColumn.Name = "Price";
 maskBoxColumn.FieldName = "Price";
 maskBoxColumn.HeaderText = "Price";
maskBoxColumn.MaskType = MaskType.Numeric;
maskBoxColumn.Mask = "C";

maskBoxColumn.EnableNullValueInput = true;

maskBoxColumn.TextAlignment = ContentAlignment.BottomRight;
maskBoxColumn.FormatString = "{0:C}"
maskBoxColumn.DataType = typeof(decimal);