Completed
Last Updated: 15 Aug 2017 11:03 by ADMIN
ADMIN
Hristo
Created on: 06 Jun 2017 12:43
Category: GridView
Type: Bug Report
2
FIX. RadGridView - the ValueChanging event of the editor elements in the grid should provide the old value as an argument
How to reproduce:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.radGridView1.DataSource = this.GetData();
            GridViewComboBoxColumn supplierColumn = new GridViewComboBoxColumn();
            supplierColumn.Name = "SupplierColumn";
            supplierColumn.HeaderText = "Supplier";
            supplierColumn.DataSource = this.GetListItemData(); ;
            supplierColumn.ValueMember = "Id";
            supplierColumn.DisplayMember = "Description";
            supplierColumn.Width = 200;
            this.radGridView1.Columns.Add(supplierColumn);
            this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.CellEditorInitialized += RadGridView1_CellEditorInitialized;
        }

        private void RadGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
            if (editor == null)
            {
                return;
            }

            RadDropDownListEditorElement element = (RadDropDownListEditorElement)editor.EditorElement;
            element.ValueChanging -= Element_ValueChanging;
            element.ValueChanging += Element_ValueChanging;
        }

        private void Element_ValueChanging(object sender, ValueChangingEventArgs e)
        {
            Console.WriteLine("ValueChanging");
            Console.WriteLine("Old " + e.OldValue);
            Console.WriteLine("New " + e.NewValue);
        }

        private object GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));

            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i));
            }

            return dt;
        }

        public List<Item> GetListItemData()
        {
            List<Item> items = new List<Item>();
            for (int i = 0; i < 10; i++)
            {
                items.Add(new Item(i, "Data" + i, DateTime.Now.AddDays(i).AddHours(i)));
            }

            return items;
        }
    }

    public class Item
    {
        public int Id { get; set; }

        public string Description { get; set; }

        public DateTime Date { get; set; }

        public Item(int id, string description, DateTime date)
        {
            this.Id = id;
            this.Description = description;
            this.Date = date;
        }
    }

Workaround: handle the TextChanging event and store locally the old value
 private void RadGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
 {
     RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
     if (editor == null)
     {
         return;
     }

     RadDropDownListEditorElement element = (RadDropDownListEditorElement)editor.EditorElement;

     element.TextChanging-= Element_TextChanging;
     element.TextChanging += Element_TextChanging;
     element.ValueChanging -= Element_ValueChanging;
     element.ValueChanging += Element_ValueChanging;
 }

 object oldValue;
 private void Element_TextChanging(object sender, TextChangingEventArgs e)
 {
     oldValue = e.OldValue;
 }

 private void Element_ValueChanging(object sender, ValueChangingEventArgs e)
 {
     Console.WriteLine("ValueChanging");
     Console.WriteLine("Old " + oldValue);
     Console.WriteLine("New " + e.NewValue);
 }

0 comments