Unplanned
Last Updated: 29 Mar 2016 11:49 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 28 Nov 2014 12:41
Category:
Type: Bug Report
1
FIX. RadDataEntry - the Parse event is not fired when RadCheckedDropDownList is used as editor
To reproduce: follow the steps introduced in http://www.telerik.com/help/winforms/dataentry-how-to-change-editor-to-drop-down-list.html. However, try to use the RadCheckedDropDownList  instead. Use the following code snippet:

private BindingList<Model> editorDS = new BindingList<Model>();
RadCheckedDropDownList checkedDDL;
BindingSource bs;

public Form1()
{
    InitializeComponent();

    this.radDataEntry1.ItemInitialized += radDataEntry1_ItemInitialized;

    radDataEntry1.EditorInitializing += radDataEntry1_EditorInitializing;
    radDataEntry1.BindingCreating += radDataEntry1_BindingCreating;
    radDataEntry1.BindingCreated += radDataEntry1_BindingCreated;

    for (int i = 0; i < 15; i++)
    {
        editorDS.Add(new Model(i,"Item " + i));
    }

    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Title", typeof(string));
    dt.Columns.Add("Models", typeof(BindingList<Model>));

    BindingList<Model> models;
    for (int i = 0; i < 5; i++)
    {
        models = new BindingList<Model>();
        for (int j = 0; j < 2; j++)
        {
            models.Add(editorDS[i + j]);
        }
        
        dt.Rows.Add(i, "Item" + i, models);
    }

    bs = new BindingSource();
    bs.DataSource = dt;
    this.radBindingNavigator1.BindingSource = bs;
    this.radDataEntry1.DataSource = bs;
}

void radDataEntry1_EditorInitializing(object sender, Telerik.WinControls.UI.EditorInitializingEventArgs e)
{
    if (e.Property.Name == "Models")
    {
        checkedDDL = new RadCheckedDropDownList();
        checkedDDL.DisplayMember = "Name";
        checkedDDL.ValueMember = "Id";
        checkedDDL.DataSource = editorDS;
        checkedDDL.Parent = this;
        checkedDDL.BindingContext = new System.Windows.Forms.BindingContext();
        e.Editor = checkedDDL;
    }
}

void radDataEntry1_BindingCreating(object sender, Telerik.WinControls.UI.BindingCreatingEventArgs e)
{
    if (e.DataMember == "Models")
    {
        e.PropertyName = "Text";
    }
}

void radDataEntry1_BindingCreated(object sender, BindingCreatedEventArgs e)
{
    if (e.DataMember == "Models")
    {
        e.Binding.FormattingEnabled = !true;
        e.Binding.Parse += Binding_Parse;
        e.Binding.Format += Binding_Format;
    }
}

private void Binding_Format(object sender, ConvertEventArgs e)
{
    BindingList<Model> models = e.Value as BindingList<Model>;
    if (models != null)
    {
        checkedDDL.CheckedItems.Clear();
        StringBuilder sb = new StringBuilder();
        foreach (Model m in models)
        {
            sb.Append(m.Name + ";");
        }
        e.Value = sb.ToString();
    }
}

private void Binding_Parse(object sender, ConvertEventArgs e)
{
    BindingList<Model> m = checkedDDL.DataSource as BindingList<Model>;
    string[] tokens;
    if (e.Value != null && m != null)
    {
        BindingList<Model> newValue = new BindingList<Model>();
        tokens = e.Value.ToString().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string token in tokens)
        {
            if (m.Any(item => item.Name == token))
            {
                Model foundModel = m.ToList().Find(item => item.Name == token);
                newValue.Add(new Model(foundModel.Id,foundModel.Name));
            }
        }

        e.Value = newValue;
    }
}

private void radDataEntry1_ItemInitialized(object sender, ItemInitializedEventArgs e)
{
    if (e.Panel.Controls[1].Text == "Models")
    {
        e.Panel.Size = new Size(350, 25);
    }
}

class Model : INotifyPropertyChanged
{
    private int id;
    private string name;

    public Model(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }

    public int Id
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
            this.OnPropertyChanged("Id");
        }
    }

    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
            this.OnPropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

The provided gif file illustrates better the obtained behavior.

Workaround: subscribe to the RadCheckedDropDownList.Validated event and call the RadCheckedDropDownList.DataBindings[0].WriteValue method. Thus, the Parse event will be fired and you can convert the input data to the appropriate format.
Attached Files:
0 comments