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

Q2 2010 Grid Exception Error

17 Answers 273 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nadia Sangiovanni
Top achievements
Rank 1
Nadia Sangiovanni asked on 27 Jul 2010, 08:36 PM
Hi Support,

I update my project to Telerik Winform Q2 2010. I encounter a problem with the new release in my project when I sort (ascending or descending) a column and then drag the column to a new position.

You can reproduce this error in the Q2 2010 Demo.
1. Select Grid View - Populate with data - Data Binding
2. Sort the Contact Name column
3. Drag the Contact Name column between the Contact Title and Address column.

This is the exception detail I obtain in my code:
System.ArgumentException was unhandled
  Message="DataItem not contains 'idesc' property name."
  Source="Telerik.WinControls"
  StackTrace:
       à Telerik.WinControls.Data.RadDataView`1.ValidateDescriptors()
       à Telerik.WinControls.Data.RadDataView`1.RebuildData(Boolean notify)
       à Telerik.WinControls.Data.RadDataView`1.RefreshOverride()
       à Telerik.WinControls.Data.RadDataView`1.OnNotifyPropertyChanged(PropertyChangedEventArgs e)
       à Telerik.WinControls.Data.RadCollectionView`1.sortDescriptors_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       à Telerik.Collections.Generic.NotifyCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
       à Telerik.WinControls.UI.GridViewSortDescriptorCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
       à Telerik.Collections.Generic.NotifyCollection`1.InsertItem(Int32 index, T item)
       à Telerik.WinControls.Data.SortDescriptorCollection.InsertItem(Int32 index, SortDescriptor item)
       à Telerik.WinControls.UI.GridViewSortDescriptorCollection.InsertItem(Int32 index, SortDescriptor item)
       à System.Collections.ObjectModel.Collection`1.Add(T item)
       à Telerik.WinControls.UI.GridViewColumn.Sort(RadSortOrder sortOrder, Boolean multiSortMode)
       à Telerik.WinControls.UI.GridViewColumn.set_SortOrder(RadSortOrder value)
       à Telerik.WinControls.UI.GridViewColumn.Initialize()
       à Telerik.WinControls.UI.GridViewDataColumn.Initialize()
       à Telerik.WinControls.UI.GridViewColumn.set_OwnerTemplate(GridViewTemplate value)
       à Telerik.WinControls.UI.GridViewColumnCollection.InsertItem(Int32 index, GridViewDataColumn item)
       à System.Collections.ObjectModel.Collection`1.Insert(Int32 index, T item)
       à Telerik.WinControls.UI.RadGridViewDragDropService.Move[T](Collection`1 collection, Int32 oldIndex, Int32 newIndex)
       à Telerik.WinControls.UI.RadGridViewDragDropService.MoveOnLeftOrRight[T](Boolean isDroppedAtLeft, Collection`1 collection, T targetItem, T draggedItem)
       à Telerik.WinControls.UI.GridHeaderCellElement.ProcessDragDrop(Point dropLocation, ISupportDrag dragObject)
       à Telerik.WinControls.RadItem.Telerik.WinControls.ISupportDrop.DragDrop(Point dropLocation, ISupportDrag dragObject)
       à Telerik.WinControls.RadDragDropService.Commit()
       à Telerik.WinControls.RadService.Stop(Boolean commit)
       à Telerik.WinControls.RadDragDropService.Telerik.WinControls.IMessageListener.PreviewMessage(Message& msg)
       à Telerik.WinControls.RadMessageFilter.NotifyGetMessageEvent(Message& msg)
       à Telerik.WinControls.RadMessageFilter.GetMessageHookProc(Int32 code, IntPtr wParam, IntPtr lParam)
       à System.Windows.Forms.UnsafeNativeMethods.GetMessageW(MSG& msg, HandleRef hWnd, Int32 uMsgFilterMin, Int32 uMsgFilterMax)
       à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.Run(Form mainForm)
       à Virtuo.Program.Main() dans C:\Projet\Virtuo\Virtuo\Program.cs:ligne 19
       à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       à System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Thank you,

Nadia

17 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Jul 2010, 08:00 AM
Hello Nadia,

I've been having the same problem and i was hoping that it was fixed in the latest internal build, but apparently it's not.

I've tryed every possible workaround i could have thinked of starting from trying to catch the column index changing event to store the sort descriptor and then set it to none and back again on index changed, but that event is not even firing...

Hopefully they will fix it soon.

Best Regards,
Emanuel Varga
0
Accepted
Julian Benkov
Telerik team
answered on 30 Jul 2010, 05:19 PM
Hi guys,

Please accept our apologies for this. The issue is logged to our Issue Tracking System and the fix will be available for Q2 2010 SP1 release, planned to the first half of August. 


All the best,

Julian Benkov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Nadia Sangiovanni
Top achievements
Rank 1
answered on 30 Jul 2010, 06:10 PM
Thank you Julian!

Best wishes
0
Huzaifa
Top achievements
Rank 1
answered on 07 Aug 2010, 07:59 AM
I am having the same issue. Hope SP1 version resolve this issue. Beside this, I think proper QA cycle should be run before release it to client. It's very simple bug, and should not present in the release. Hope you understand, what i want to say.


Regards,
Huzaifa
0
Julian Benkov
Telerik team
answered on 12 Aug 2010, 03:30 PM
Hi Huzaifa,

Sorry for the introduced inconvenience.

Please upgrade to our latest release Q2 2010 Service Pack 1 and let us know if you encounter any issues.

Kind regards,
Julian Benkov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
DDM Consulting
Top achievements
Rank 2
answered on 28 Sep 2010, 03:15 PM
i'm on Q2 2010 SP2 and i still have the problem,


System.ArgumentException occurred
  Message="DataItem not contains 'someColumn' property name."
  Source="Telerik.WinControls"
  StackTrace:
       at Telerik.WinControls.Data.RadDataView`1.ValidateDescriptors()
       at Telerik.WinControls.Data.RadDataView`1.RebuildData(Boolean notify)
       at Telerik.WinControls.Data.RadDataView`1.AddItem(TDataItem item)
       at Telerik.WinControls.Data.RadDataView`1.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
       at Telerik.WinControls.UI.TemplateDataView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
       at Telerik.WinControls.Data.RadCollectionView`1.source_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at Telerik.WinControls.Data.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at Telerik.WinControls.Data.RadListSource`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       at Telerik.WinControls.Data.RadListSource`1.InsertItem(Int32 index, TDataItem item)
       at Telerik.WinControls.Data.RadListSource`1.AddNew()
       at Telerik.WinControls.UI.GridViewRowCollection.AddNew()
       at myMethod()
  InnerException:
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Sep 2010, 03:35 PM
Hello DDM Consulting,

Can you please provide a small example, or a test scenario in which this error occurs?

Because i had this same problem before and i can report that it has been fixed.

Best Regards,
Emanuel Varga
0
DDM Consulting
Top achievements
Rank 2
answered on 28 Sep 2010, 04:25 PM
Hello,

This is the code that throws the exception. The AddAgentToGrid method is being called from another thread hence the call to the delegate.

 private delegate void delegateAddAgentToGrid(Agent agent);
 private void AddAgentToGrid(Agent agent)
 {
     if (InvokeRequired)
         Invoke(new delegateAddAgentToGrid(AddAgentToGrid), agent);
     else
     {
         if (agent != null
             && agent != Dispatcher.Instance.CurrentAgent)
         {
             GridViewRowInfo row = grdAgentList.Rows.AddNew(); // Here I get the ArgumentException thrown
             row.Tag = agent;
             UpdateRow(row, agent);
         }
     }
 }
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Sep 2010, 04:59 PM
Hello again,

I'm guessing you are using a bounded grid, i would suggest to try handling the event UserAddingRow, use e.Cancel = true; and create the object manually and add it to the collection from there.

Please let me know if this helped, or please tell me what should i create in order to reproduce your issue.

Best Regards,
Emanuel Varga
0
DDM Consulting
Top achievements
Rank 2
answered on 29 Sep 2010, 09:04 AM
Hello,

No i add/remove/update rows inline and manually, i never set the datasource nor add a databind to the gridview.

KR

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 09:57 AM
Hello again,

Try the following code and please let me know what is different in your example:
namespace TestGridException
{
    using System;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
 
        private SynchronizationContext synchronizationContext;
 
        private int count;
 
        public Form1()
        {
            InitializeComponent();
            synchronizationContext = SynchronizationContext.Current;
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var addButton = new RadButton();
            addButton.Text = "Add from a different thread";
            addButton.Click += new EventHandler(addButton_Click);
            addButton.Dock = DockStyle.Bottom;
            this.Controls.Add(addButton);
 
            radGridView1 = new RadGridView();
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            var column1 = new GridViewMaskBoxColumn("Id");
            radGridView1.Columns.Add(column1);
            var column2 = new GridViewTextBoxColumn("Name");
            radGridView1.Columns.Add(column2);
 
            radGridView1.Size = new Size(this.Width, this.Height - 62);
            radGridView1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            this.Controls.Add(radGridView1);
        }
 
        void addButton_Click(object sender, EventArgs e)
        {
            var thread = new Thread(AddDataFromADifferentThread);
            thread.IsBackground = true;
            thread.Start(new Test(count, "item" + count++));
        }
 
        private void AddDataFromADifferentThread(object dataObject)
        {
            if (this.radGridView1.InvokeRequired)
            {
                synchronizationContext.Post(AddDataFromADifferentThread, dataObject);
                return;
            }
 
            var row = radGridView1.Rows.AddNew();
            row.Tag = (Test)dataObject;
            UpdateRow(row, (Test)dataObject);
        }
 
        private void UpdateRow(GridViewRowInfo row, Test test)
        {
            row.Cells["Id"].Value = test.Id;
            row.Cells["Name"].Value = test.Name;
        }
    }
 
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public Test(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

Please let me know if this helped, or please tell point out what else should i add here in order to better understand what and where is the problem

Best Regards,
Emanuel Varga
0
DDM Consulting
Top achievements
Rank 2
answered on 29 Sep 2010, 01:43 PM
Hi Emanuel

I have tried adapting your code more or less to the same logic as used in my project, but alas i fail to reproduce the same exception...

Here's what i've got so far...

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
 
namespace TestGridException
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        private Dictionary<GridViewRowInfo, System.Windows.Forms.Timer> timerPerRow = new Dictionary<GridViewRowInfo, System.Windows.Forms.Timer>();
        private int count;
 
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var addButton = new RadButton();
            addButton.Text = "Add from a different thread";
            addButton.Click += new EventHandler(addButton_Click);
            addButton.Dock = DockStyle.Bottom;
            Controls.Add(addButton);
 
            radGridView1 = new RadGridView();
            radGridView1.CellFormatting += radgridview1_ViewCellFormatting;
            radGridView1.TableElement.RowHeight = 24;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            var column2 = new GridViewTextBoxColumn("colTime");
            radGridView1.Columns.Add(column2);
 
            var column3 = new GridViewTextBoxColumn("colID");
            radGridView1.Columns.Add(column3);
 
            var column4 = new GridViewTextBoxColumn("colFirstName");
            radGridView1.Columns.Add(column4);
 
            var column5 = new GridViewTextBoxColumn("colLastName");
            radGridView1.Columns.Add(column5);
 
            var column6 = new GridViewTextBoxColumn("colEmployeeID");
            radGridView1.Columns.Add(column6);
 
            var column7 = new GridViewTextBoxColumn("colTelephone");
            radGridView1.Columns.Add(column7);
 
            radGridView1.Size = new Size(Width, Height - 62);
            radGridView1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
 
            Controls.Add(radGridView1);
        }
 
        void addButton_Click(object sender, EventArgs e)
        {
            BackgroundWorker bgworker = new BackgroundWorker();
            bgworker.DoWork += bgworker_DoWork;
            Test newTest = new Test
                               {
                                   Id = count,
                                   FirstName = "item",
                                   LastName = count++ + "",
                                   Telephone = "555-123456789",
                                   MyStatus = Status.Open
                               }.Start();
            newTest.onStateChanged += newTest_onStateChanged;
            bgworker.RunWorkerAsync(newTest);
        }
 
        void newTest_onStateChanged(Test test)
        {
            UpdateTest(test);
        }
 
        void bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            AddDataFromADifferentThread(e.Argument);
        }
 
        private delegate void delegateAddDataFromADifferentThread(Test test);
        private void AddDataFromADifferentThread(object test)
        {
            if (InvokeRequired)
                Invoke(new delegateAddDataFromADifferentThread(AddDataFromADifferentThread), (Test) test);
            else
                AddTestToGrid((Test) test);
        }
 
        private void createTimerForRow(GridViewRowInfo gvri)
        {
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Tag = gvri;
            timer.Tick += onCallTicker_Tick;
            timerPerRow.Add(gvri, timer);
            timer.Start();
        }
 
        void onCallTicker_Tick(object sender, EventArgs e)
        {
            if (sender != null
                 && sender is Timer
                 && ((Timer)sender).Tag != null
                 && ((Timer)sender).Tag is GridViewRowInfo
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag != null
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag is Test
                 && ((Test)((GridViewRowInfo)((Timer)sender).Tag).Tag).MyStatus == Status.Open)
            {
                foreach (GridViewRowInfo row in radGridView1.Rows)
                {
                    if (((Timer)sender).Tag == row)
                    {
                        TimeSpan ti = (DateTime.Now - ((Test)row.Tag).CreationTime);
                        row.Cells["colTime"].Value = string.Format("{0:##}{3}{1:#0}:{2:00}", ti.Hours, ti.Minutes,
                                                                   ti.Seconds, ti.Hours > 0 ? ":" : string.Empty);
                        row.InvalidateRow();
                        return;
                    }
                }
            }
        }
 
        private void radgridview1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo == null
                || e.CellElement.RowInfo.Tag == null) return;
            if (e.CellElement.ColumnInfo == radGridView1.Columns["colTime"])
            {
                TimeSpan ti = (DateTime.Now - ((Test)e.CellElement.RowInfo.Tag).CreationTime);
                e.CellElement.Value =
                    string.Format("{0:##}{3}{1:#0}:{2:00}",
                                    ti.Hours, ti.Minutes, ti.Seconds,
                                    ti.Hours > 0 ? ":" : string.Empty);
            }
        }
 
        private delegate void delegateRemoveAgentFromGrid(GridViewRowInfo row);
        private void RemoveTestfromGrid(GridViewRowInfo row)
        {
            if (InvokeRequired)
                Invoke(new delegateRemoveAgentFromGrid(RemoveTestfromGrid), row);
            else
            {
                timerPerRow[row].Stop();
                timerPerRow.Remove(row);
                radGridView1.Rows.Remove(row);
            }
        }
 
        private delegate void delegateAddTestToGrid(Test test);
        private void AddTestToGrid(Test test)
        {
            if (InvokeRequired)
                Invoke(new delegateAddTestToGrid(AddTestToGrid), test);
            else
            {
 
                GridViewRowInfo row = radGridView1.Rows.AddNew();
                row.Tag = test;
                createTimerForRow(row);
                UpdateRow(row, test);
            }
        }
 
        private delegate void delegateUpdateRow(GridViewRowInfo row, Test test);
        private void UpdateRow(GridViewRowInfo row, Test test)
        {
            if (InvokeRequired)
                Invoke(new delegateUpdateRow(UpdateRow), row, test);
            else
            {
                row.Cells["colID"].Value = test.Id;
                row.Cells["colFirstName"].Value = test.FirstName;
                row.Cells["colLastName"].Value = test.LastName;
                row.Cells["colEmployeeID"].Value = test.EmployeeID;
                row.Cells["colDN"].Value = test.Telephone;
            }
        }
 
        delegate void UpdateTestInvoke(Test test);
        public void UpdateTest(Test test)
        {
            if (IsDisposed)
                return;
            if (InvokeRequired)
            {
                UpdateTestInvoke updateAgentInvoke = UpdateTestDelegate;
                updateAgentInvoke.Invoke(test);
            }
            else
                UpdateTestDelegate(test);
        }
 
        private void UpdateTestDelegate(Test test)
        {
            GridViewRowInfo row = GetRow(test);
            if (row != null)
                if (test.MyStatus == Status.Closed)
                    RemoveTestfromGrid(row);
                else
                    UpdateRow(row, test);
            else
                AddTestToGrid(test);
        }
 
        private delegate GridViewRowInfo delegateGetRow(Test test);
        private GridViewRowInfo GetRow(Test thisTest)
        {
            if (InvokeRequired)
                return (GridViewRowInfo) Invoke(new delegateGetRow(GetRow), thisTest);
            else
                try
                {
                    if (thisTest == null)
                        return null;
                    foreach (GridViewRowInfo gvdri in radGridView1.Rows)
                        if (gvdri.Tag != null
                            && gvdri.Tag is Test
                            && gvdri.Tag == thisTest)
                            return gvdri;
                    return null;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                    return null;
                }
        }
    }
 
    public class Test
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Telephone { get; set; }
        public string EmployeeID { get { return string.Format("{0}_{1} {2} @ {3}", Id, FirstName, LastName, Telephone); } }
        private Status myStatus;
        public Status MyStatus
        {
            get { return myStatus; }
            set { myStatus = value; if (onStateChanged != null) onStateChanged.Invoke(this); }
        }
        private DateTime myCreationTime;
        public DateTime CreationTime { get { return myCreationTime; } }
        private Timer countDownTimer = new Timer();
        public Test()
        {
            myCreationTime = DateTime.Now;
        }
        public delegate void StateChangedHandler(Test test);
        public event StateChangedHandler onStateChanged;
        
        /// <summary>
        /// this method starts changing the MyStatus property at random.
        /// </summary>
        /// <returns>This Test objet</returns>
        internal Test Start()
        {
            countDownTimer.Interval = (new Random().Next(5, 10))*1000;
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Start();
            return this;
        }
 
        void countDownTimer_Opening_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Open;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Interval = (new Random().Next(5, 10))*1000;
            countDownTimer.Start();
        }
 
        void countDownTimer_Closing_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Closed;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick += countDownTimer_Opening_Tick;
            countDownTimer.Interval = (new Random().Next(1, 5))*1000;
            countDownTimer.Start();
        }
    }
 
    public enum Status{ Open, Closed }
}
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 02:08 PM
Hello again,

This is working for me, but you've made one mistake, in the method:
private void UpdateRow(GridViewRowInfo row, Test test)
        {
            if (InvokeRequired)
                Invoke(new delegateUpdateRow(UpdateRow), row, test);
            else
            {
                row.Cells["colID"].Value = test.Id;
                row.Cells["colFirstName"].Value = test.FirstName;
                row.Cells["colLastName"].Value = test.LastName;
                row.Cells["colEmployeeID"].Value = test.EmployeeID;
                row.Cells["colTelephone"].Value = test.Telephone;
            }
        }

because it was throwing a null reference exception.

Honestly i would suggest using SynchronizationContext instead of using all those delegates, it would make the code cleaner, at least in my point of view, if would like i would suggest taking a look at this:

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
 
namespace TestGridException
{
    using System;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
    using Timer = System.Windows.Forms.Timer;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        private SynchronizationContext synchronizationContext;
        private Dictionary<GridViewRowInfo, System.Windows.Forms.Timer> timerPerRow = new Dictionary<GridViewRowInfo, System.Windows.Forms.Timer>();
        private int count;
 
        public Form1()
        {
            InitializeComponent();
            synchronizationContext = SynchronizationContext.Current;
            Load += Form1_Load;
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var addButton = new RadButton();
            addButton.Text = "Add from a different thread";
            addButton.Click += new EventHandler(addButton_Click);
            addButton.Dock = DockStyle.Bottom;
            Controls.Add(addButton);
 
            radGridView1 = new RadGridView();
            radGridView1.CellFormatting += radgridview1_ViewCellFormatting;
            radGridView1.TableElement.RowHeight = 24;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            var column2 = new GridViewTextBoxColumn("colTime");
            radGridView1.Columns.Add(column2);
 
            var column3 = new GridViewTextBoxColumn("colID");
            radGridView1.Columns.Add(column3);
 
            var column4 = new GridViewTextBoxColumn("colFirstName");
            radGridView1.Columns.Add(column4);
 
            var column5 = new GridViewTextBoxColumn("colLastName");
            radGridView1.Columns.Add(column5);
 
            var column6 = new GridViewTextBoxColumn("colEmployeeID");
            radGridView1.Columns.Add(column6);
 
            var column7 = new GridViewTextBoxColumn("colTelephone");
            radGridView1.Columns.Add(column7);
 
            radGridView1.Size = new Size(Width, Height - 62);
            radGridView1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
 
            Controls.Add(radGridView1);
        }
 
        void addButton_Click(object sender, EventArgs e)
        {
            BackgroundWorker bgworker = new BackgroundWorker();
            bgworker.DoWork += bgworker_DoWork;
            Test newTest = new Test
            {
                Id = count,
                FirstName = "item",
                LastName = count++ + "",
                Telephone = "555-123456789",
                MyStatus = Status.Open
            }.Start();
            newTest.onStateChanged += newTest_onStateChanged;
            bgworker.RunWorkerAsync(newTest);
        }
 
        void newTest_onStateChanged(Test test)
        {
            if (SynchronizationContext.Current != synchronizationContext)
            {
                synchronizationContext.Post((o) => UpdateTest(test), null);
            }
            else
            {
                UpdateTest(test);
            }
        }
 
        void bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            synchronizationContext.Post(AddTestToGrid, (Test)e.Argument);
        }
 
        private void createTimerForRow(GridViewRowInfo gvri)
        {
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Tag = gvri;
            timer.Tick += onCallTicker_Tick;
            timerPerRow.Add(gvri, timer);
            timer.Start();
        }
 
        void onCallTicker_Tick(object sender, EventArgs e)
        {
            if (sender != null
                 && sender is Timer
                 && ((Timer)sender).Tag != null
                 && ((Timer)sender).Tag is GridViewRowInfo
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag != null
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag is Test
                 && ((Test)((GridViewRowInfo)((Timer)sender).Tag).Tag).MyStatus == Status.Open)
            {
                foreach (GridViewRowInfo row in radGridView1.Rows)
                {
                    if (((Timer)sender).Tag == row)
                    {
                        TimeSpan ti = (DateTime.Now - ((Test)row.Tag).CreationTime);
                        row.Cells["colTime"].Value = string.Format("{0:##}{3}{1:#0}:{2:00}", ti.Hours, ti.Minutes,
                                                                   ti.Seconds, ti.Hours > 0 ? ":" : string.Empty);
                        row.InvalidateRow();
                        return;
                    }
                }
            }
        }
 
        private void radgridview1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo == null
                || e.CellElement.RowInfo.Tag == null)
                return;
            if (e.CellElement.ColumnInfo == radGridView1.Columns["colTime"])
            {
                TimeSpan ti = (DateTime.Now - ((Test)e.CellElement.RowInfo.Tag).CreationTime);
                e.CellElement.Value =
                    string.Format("{0:##}{3}{1:#0}:{2:00}",
                                    ti.Hours, ti.Minutes, ti.Seconds,
                                    ti.Hours > 0 ? ":" : string.Empty);
            }
        }
 
        private void RemoveTestfromGrid(GridViewRowInfo row)
        {
            timerPerRow[row].Stop();
            timerPerRow.Remove(row);
            radGridView1.Rows.Remove(row);
        }
 
        private void AddTestToGrid(object obj)
        {
            var test = (Test)obj;
            GridViewRowInfo row = radGridView1.Rows.AddNew();
            row.Tag = test;
            createTimerForRow(row);
            UpdateRow(row, test);
        }
 
        private void UpdateRow(GridViewRowInfo row, Test test)
        {
            row.Cells["colID"].Value = test.Id;
            row.Cells["colFirstName"].Value = test.FirstName;
            row.Cells["colLastName"].Value = test.LastName;
            row.Cells["colEmployeeID"].Value = test.EmployeeID;
            row.Cells["colTelephone"].Value = test.Telephone;
        }
 
        public void UpdateTest(Test test)
        {
            if (IsDisposed)
                return;
 
            UpdateTestDelegate(test);
        }
 
        private void UpdateTestDelegate(Test test)
        {
            GridViewRowInfo row = GetRow(test);
            if (row != null)
                if (test.MyStatus == Status.Closed)
                    RemoveTestfromGrid(row);
                else
                    UpdateRow(row, test);
            else
                AddTestToGrid(test);
        }
 
        private GridViewRowInfo GetRow(Test thisTest)
        {
            try
            {
                if (thisTest == null)
                    return null;
                foreach (GridViewRowInfo gvdri in radGridView1.Rows)
                    if (gvdri.Tag != null
                        && gvdri.Tag is Test
                        && gvdri.Tag == thisTest)
                        return gvdri;
                return null;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
                return null;
            }
        }
    }
 
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
 
        public string FirstName
        {
            get;
            set;
        }
 
        public string LastName
        {
            get;
            set;
        }
 
        public string Telephone
        {
            get;
            set;
        }
 
        public string EmployeeID
        {
            get
            {
                return string.Format("{0}_{1} {2} @ {3}", Id, FirstName, LastName, Telephone);
            }
        }
 
        private Status myStatus;
 
        public Status MyStatus
        {
            get
            {
                return myStatus;
            }
            set
            {
                myStatus = value;
                if (onStateChanged != null)
                    onStateChanged.Invoke(this);
            }
        }
 
        private DateTime myCreationTime;
 
        public DateTime CreationTime
        {
            get
            {
                return myCreationTime;
            }
        }
 
        private Timer countDownTimer = new Timer();
 
        public Test()
        {
            myCreationTime = DateTime.Now;
        }
 
        public delegate void StateChangedHandler(Test test);
        public event StateChangedHandler onStateChanged;
 
        /// <summary>
        /// this method starts changing the MyStatus property at random.
        /// </summary>
        /// <returns>This Test objet</returns>
        internal Test Start()
        {
            countDownTimer.Interval = (new Random().Next(5, 10)) * 1000;
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Start();
            return this;
        }
 
        void countDownTimer_Opening_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Open;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Interval = (new Random().Next(5, 10)) * 1000;
            countDownTimer.Start();
        }
 
        void countDownTimer_Closing_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Closed;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick += countDownTimer_Opening_Tick;
            countDownTimer.Interval = (new Random().Next(1, 5)) * 1000;
            countDownTimer.Start();
        }
    }
 
    public enum Status
    {
        Open,
        Closed
    }
}

Please let me know if there is anything else i can do to help,

Best Regards,
Emanuel Varga
0
DDM Consulting
Top achievements
Rank 2
answered on 29 Sep 2010, 03:34 PM
Emanuel,

i found out how to reproduce the exception.


see code below.

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
 
namespace TestGridException
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        private Dictionary<GridViewRowInfo, System.Windows.Forms.Timer> timerPerRow = new Dictionary<GridViewRowInfo, System.Windows.Forms.Timer>();
        private int count;
 
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var addButton = new RadButton();
            addButton.Text = "Add from a different thread";
            addButton.Click += new EventHandler(addButton_Click);
            addButton.Dock = DockStyle.Bottom;
            Controls.Add(addButton);
 
            radGridView1 = new RadGridView();
            radGridView1.CellFormatting += radgridview1_ViewCellFormatting;
            radGridView1.TableElement.RowHeight = 24;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            var column2 = new GridViewTextBoxColumn("colTime");
            radGridView1.Columns.Add(column2);
 
            var column3 = new GridViewTextBoxColumn("colID");
            radGridView1.Columns.Add(column3);
 
            var column4 = new GridViewTextBoxColumn("colFirstName");
            radGridView1.Columns.Add(column4);
 
            var column5 = new GridViewTextBoxColumn("colLastName");
            radGridView1.Columns.Add(column5);
 
            var column6 = new GridViewTextBoxColumn("colEmployeeID");
            radGridView1.Columns.Add(column6);
 
            var column7 = new GridViewTextBoxColumn("colTelephone");
            radGridView1.Columns.Add(column7);
 
            ///
            /// Because of the sortfield on a column that is not in the current columns collection we will see the same exception.
            ///
            GridSortField gridSortField1 = new GridSortField();
            gridSortField1.FieldAlias = "someColumn";
            gridSortField1.FieldName = "someColumn";
            gridSortField1.PropertyName = "someColumn";
            gridSortField1.SortOrder = RadSortOrder.Ascending;
            radGridView1.MasterTemplate.SortDescriptors.AddRange(new Telerik.WinControls.Data.SortDescriptor[] {
            gridSortField1});
            ///
            /// end of error code!
            ///
            radGridView1.Size = new Size(Width, Height - 62);
            radGridView1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
 
            Controls.Add(radGridView1);
        }
 
        void addButton_Click(object sender, EventArgs e)
        {
            BackgroundWorker bgworker = new BackgroundWorker();
            bgworker.DoWork += bgworker_DoWork;
            Test newTest = new Test
                               {
                                   Id = count,
                                   FirstName = "item",
                                   LastName = count++ + "",
                                   Telephone = "555-123456789",
                                   MyStatus = Status.Open
                               }.Start();
            newTest.onStateChanged += newTest_onStateChanged;
            bgworker.RunWorkerAsync(newTest);
        }
 
        void newTest_onStateChanged(Test test)
        {
            UpdateTest(test);
        }
 
        void bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            AddDataFromADifferentThread(e.Argument);
        }
 
        private delegate void delegateAddDataFromADifferentThread(Test test);
        private void AddDataFromADifferentThread(object test)
        {
            if (InvokeRequired)
                Invoke(new delegateAddDataFromADifferentThread(AddDataFromADifferentThread), (Test) test);
            else
                AddTestToGrid((Test) test);
        }
 
        private void createTimerForRow(GridViewRowInfo gvri)
        {
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Tag = gvri;
            timer.Tick += onCallTicker_Tick;
            timerPerRow.Add(gvri, timer);
            timer.Start();
        }
 
        void onCallTicker_Tick(object sender, EventArgs e)
        {
            if (sender != null
                 && sender is Timer
                 && ((Timer)sender).Tag != null
                 && ((Timer)sender).Tag is GridViewRowInfo
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag != null
                 && ((GridViewRowInfo)((Timer)sender).Tag).Tag is Test
                 && ((Test)((GridViewRowInfo)((Timer)sender).Tag).Tag).MyStatus == Status.Open)
            {
                foreach (GridViewRowInfo row in radGridView1.Rows)
                {
                    if (((Timer)sender).Tag == row)
                    {
                        TimeSpan ti = (DateTime.Now - ((Test)row.Tag).CreationTime);
                        row.Cells["colTime"].Value = string.Format("{0:##}{3}{1:#0}:{2:00}", ti.Hours, ti.Minutes,
                                                                   ti.Seconds, ti.Hours > 0 ? ":" : string.Empty);
                        row.InvalidateRow();
                        return;
                    }
                }
            }
        }
 
        private void radgridview1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo == null
                || e.CellElement.RowInfo.Tag == null) return;
            if (e.CellElement.ColumnInfo == radGridView1.Columns["colTime"])
            {
                TimeSpan ti = (DateTime.Now - ((Test)e.CellElement.RowInfo.Tag).CreationTime);
                e.CellElement.Value =
                    string.Format("{0:##}{3}{1:#0}:{2:00}",
                                    ti.Hours, ti.Minutes, ti.Seconds,
                                    ti.Hours > 0 ? ":" : string.Empty);
            }
        }
 
        private delegate void delegateRemoveAgentFromGrid(GridViewRowInfo row);
        private void RemoveTestfromGrid(GridViewRowInfo row)
        {
            if (InvokeRequired)
                Invoke(new delegateRemoveAgentFromGrid(RemoveTestfromGrid), row);
            else
            {
                timerPerRow[row].Stop();
                timerPerRow.Remove(row);
                radGridView1.Rows.Remove(row);
            }
        }
 
        private delegate void delegateAddTestToGrid(Test test);
        private void AddTestToGrid(Test test)
        {
            if (InvokeRequired)
                Invoke(new delegateAddTestToGrid(AddTestToGrid), test);
            else
            {
 
                GridViewRowInfo row = radGridView1.Rows.AddNew();
                row.Tag = test;
                createTimerForRow(row);
                UpdateRow(row, test);
            }
        }
 
        private delegate void delegateUpdateRow(GridViewRowInfo row, Test test);
        private void UpdateRow(GridViewRowInfo row, Test test)
        {
            if (InvokeRequired)
                Invoke(new delegateUpdateRow(UpdateRow), row, test);
            else
            {
                row.Cells["colID"].Value = test.Id;
                row.Cells["colFirstName"].Value = test.FirstName;
                row.Cells["colLastName"].Value = test.LastName;
                row.Cells["colEmployeeID"].Value = test.EmployeeID;
                row.Cells["colTelephone"].Value = test.Telephone;
            }
        }
 
        delegate void UpdateTestInvoke(Test test);
        public void UpdateTest(Test test)
        {
            if (IsDisposed)
                return;
            if (InvokeRequired)
            {
                UpdateTestInvoke updateAgentInvoke = UpdateTestDelegate;
                updateAgentInvoke.Invoke(test);
            }
            else
                UpdateTestDelegate(test);
        }
 
        private void UpdateTestDelegate(Test test)
        {
            GridViewRowInfo row = GetRow(test);
            if (row != null)
                if (test.MyStatus == Status.Closed)
                    RemoveTestfromGrid(row);
                else
                    UpdateRow(row, test);
            else
                AddTestToGrid(test);
        }
 
        private delegate GridViewRowInfo delegateGetRow(Test test);
        private GridViewRowInfo GetRow(Test thisTest)
        {
            if (InvokeRequired)
                return (GridViewRowInfo) Invoke(new delegateGetRow(GetRow), thisTest);
            else
                try
                {
                    if (thisTest == null)
                        return null;
                    foreach (GridViewRowInfo gvdri in radGridView1.Rows)
                        if (gvdri.Tag != null
                            && gvdri.Tag is Test
                            && gvdri.Tag == thisTest)
                            return gvdri;
                    return null;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                    return null;
                }
        }
    }
 
    public class Test
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Telephone { get; set; }
        public string EmployeeID { get { return string.Format("{0}_{1} {2} @ {3}", Id, FirstName, LastName, Telephone); } }
        private Status myStatus;
        public Status MyStatus
        {
            get { return myStatus; }
            set { myStatus = value; if (onStateChanged != null) onStateChanged.Invoke(this); }
        }
        private DateTime myCreationTime;
        public DateTime CreationTime { get { return myCreationTime; } }
        private Timer countDownTimer = new Timer();
        public Test()
        {
            myCreationTime = DateTime.Now;
        }
        public delegate void StateChangedHandler(Test test);
        public event StateChangedHandler onStateChanged;
        
        /// <summary>
        /// this method starts changing the MyStatus property at random.
        /// </summary>
        /// <returns>This Test objet</returns>
        internal Test Start()
        {
            countDownTimer.Interval = (new Random().Next(5, 10))*1000;
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Start();
            return this;
        }
 
        void countDownTimer_Opening_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Open;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Opening_Tick;
            countDownTimer.Tick += countDownTimer_Closing_Tick;
            countDownTimer.Interval = (new Random().Next(5, 10))*1000;
            countDownTimer.Start();
        }
 
        void countDownTimer_Closing_Tick(object sender, EventArgs e)
        {
            MyStatus = Status.Closed;
            countDownTimer.Stop();
            countDownTimer.Tick -= countDownTimer_Closing_Tick;
            countDownTimer.Tick += countDownTimer_Opening_Tick;
            countDownTimer.Interval = (new Random().Next(1, 5))*1000;
            countDownTimer.Start();
        }
    }
 
    public enum Status{ Open, Closed }
}

Now by not doing this the issue is resolved, but please pass this minor bug to development so they can handle any exceptions related to adding a sortfield that is not a field of the gridview.

Kind Regards, and many thanks!
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 03:43 PM
Hello again,

This is normal to happen when you try to perform a search on a column that does not belong to the grid, although i agree it would have been nice if a normal exception like: "The Column xxx does not belong to the ColumnsCollection", but all the same i glad we were able to find the problem.

Glad to be able to help,

Best Regards,
Emanuel Varga
0
Julian Benkov
Telerik team
answered on 01 Oct 2010, 01:14 PM
Hello DDM Consulting,

The GridSortField is an obsolete API, so please use the new SortDescriptors to apply sorting data operation in RadGridView control. The following code snippet
 
GridSortField gridSortField1 = new GridSortField();
gridSortField1.FieldAlias = "someColumn";
gridSortField1.FieldName = "someColumn";
gridSortField1.PropertyName = "someColumn";
gridSortField1.SortOrder = RadSortOrder.Ascending;
radGridView1.MasterTemplate.SortDescriptors.AddRange(new Telerik.WinControls.Data.SortDescriptor[] {
gridSortField1});

can be changed to this one:

SortDescriptor sortDescriptor = new SortDescriptor("someColumn", ListSortDirection.Ascending);
this.radGridView1.SortDescriptors.Add(sortDescriptor);

or you can directly add to the sort collection by using only parameters:

this.radGridView1.SortDescriptors.Add("someColumn", ListSortDirection.Ascending);


Greetings,

Julian Benkov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
DDM Consulting
Top achievements
Rank 2
answered on 01 Oct 2010, 02:19 PM
Julina,
Thank you for the info!
KR
Tags
GridView
Asked by
Nadia Sangiovanni
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Julian Benkov
Telerik team
Nadia Sangiovanni
Top achievements
Rank 1
Huzaifa
Top achievements
Rank 1
DDM Consulting
Top achievements
Rank 2
Share this question
or