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

updating problem

8 Answers 191 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ali
Top achievements
Rank 1
Ali asked on 29 May 2013, 05:40 PM
Hi,
i wanted to update DataTable and show the data in a grid
i update DataTable in a timer with the interval of 1 second

it updates successfully but when i drag the column header and the update timer ticks the following error occurs

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=Telerik.WinControls.GridView
  StackTrace:
       at Telerik.WinControls.UI.RadGridViewDragDropService.GetDragImageHint(ContentAlignment textAlignment, Bitmap hintImage, RectangleF textRectangle, Int32 hintImageWidth)
       at Telerik.WinControls.UI.GridHeaderCellElement.GetDragHintCore()
       at Telerik.WinControls.RadItem.Telerik.WinControls.ISupportDrag.GetDragHint()
       at Telerik.WinControls.RadDragDropService.PrepareContext()
       at Telerik.WinControls.RadDragDropService.HandleMouseMove(Point mousePos)
       at Telerik.WinControls.UI.RadGridViewDragDropService.HandleMouseMove(Point mousePosition)
       at Telerik.WinControls.RadDragDropService.Telerik.WinControls.IMessageListener.PreviewMessage(Message& msg)
       at Telerik.WinControls.RadMessageFilter.NotifyGetMessageEvent(Message& msg)
       at Telerik.WinControls.RadMessageFilter.GetMessageHookProc(Int32 code, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.UnsafeNativeMethods.GetMessageW(MSG& msg, HandleRef hWnd, Int32 uMsgFilterMin, Int32 uMsgFilterMax)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at RadControlsWinFormsApp1.Program.Main() in Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


here is my code:
private void RadRibbonForm1_Load(object sender, EventArgs e)
{
        DataTable Data = new DataTable();
        Data.Columns.Add("field1");
        Data.Columns.Add("field2");
        Data.Columns.Add("field3");
        Data.Columns.Add("field4");
        Data.Columns.Add("field5");
        Data.Columns.Add("field6");
        Data.Columns.Add("field7");
        Data.Columns.Add("field8");
        Data.Columns.Add("field9");
       radGridView1.DataSource = Data;
}
void fillData()
{
lock (Data)
            {
                Data.BeginLoadData();
 
                if (adapter.SelectCommand.Connection.State == ConnectionState.Closed)
                    adapter.SelectCommand.Connection.Open();
                SqlDataReader reader = adapter.SelectCommand.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        var v1 = reader.GetInt64(0);
                        var v2 = reader.GetInt32(1);
                        var v3 = reader.GetDouble(2);
                        var v4 = reader.GetDouble(3);
                        var v5 = reader.GetString(4);
                        var v6 = reader.GetString(5);
                        var v7 = reader.GetFloat(6);
                        var v8 = reader.GetFloat(7);
                        var v9 = reader.GetInt32(8);
                        Data.LoadDataRow(new object[] { v1, v2, v3, v4, v5, v6, v7, v8, v9 }, false);
                    }
 
                    Data.EndLoadData();
                    reader.Close();
                    DataRow[] deletedrows = Data.Select(string.Empty, string.Empty, DataViewRowState.Unchanged);
                    foreach (DataRow item in deletedrows)
                        item.Delete();
 
                    Data.AcceptChanges();
 
                }
                catch (Exception)
                {
                    Data.EndLoadData();
                    reader.Close();
                }
            }
}
 
 private void timer1_Tick(object sender, EventArgs e)
        {
            fillData();
            this.radGridView1.MasterTemplate.Refresh(null);
        }

8 Answers, 1 is accepted

Sort by
0
Ali
Top achievements
Rank 1
answered on 03 Jun 2013, 07:26 AM
isn't there any help?
please help me
i need your help to do this?
thanks
0
Peter
Telerik team
answered on 03 Jun 2013, 06:36 PM
Hi Ali,

Thank you for your question.

Reordering of the columns is not supported when updating the underlying source and you should suspend the columns reordering functionality when using the grid in this mode:
radGridView1.AllowColumnReorder = false;

Should you have any other questions, do not hesitate to ask.

Regards, Peter
Telerik
RadChart for WinForms is obsolete. Now what?
0
Ali
Top achievements
Rank 1
answered on 03 Jun 2013, 07:14 PM
not working
could you please give me a sample to update with the interval of 1 second?
(i prefer background worker instead of timer)
thanks
0
Peter
Telerik team
answered on 04 Jun 2013, 04:15 PM
Hello,

Please refer to the attached project that demonstrates the idea from the previous answer. 

I did not send a sample with BackgroundWorker because all UI controls in .Net framework are NOT thread safe. This is a framework limitation and you should always use the
Invoke method when accessing them from a thread different than the UI thread and all user interface changes in WinForms should happen in the UI thread.

Regards,
Peter
Telerik
RadChart for WinForms is obsolete. Now what?
0
Ali
Top achievements
Rank 1
answered on 05 Jun 2013, 06:04 AM
thank you very much
that's really bad!
if i use timer in my project the forms will hangs and it's not working for me
i have to use backgroundworker in my project
unfortunately it seems that i cant use Telerik anymore :(
0
Peter
Telerik team
answered on 07 Jun 2013, 02:47 PM
Hi Ali,

As I mentioned this is not related to Telerik controls and all UI controls in .Net framework are NOT thread safe and you cannot update the UI from another thread without Invoke.

Regards,
Peter
Telerik
RadChart for WinForms is obsolete. Now what?
0
Ali
Top achievements
Rank 1
answered on 08 Jun 2013, 02:15 PM
thanks
could you please give me a sample code with invoke included to see how it's working?
thanks
0
Peter
Telerik team
answered on 12 Jun 2013, 01:35 PM
Hello,

Here are a sample code the demonstrates the invoke usage:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     this.radGridView1.Invoke(new Action<ProgressChangedEventArgs>(OnProgressChanged), e);
 }
 
 private void OnProgressChanged(ProgressChangedEventArgs e)
 {
         try
         {
             //add records to grid
         }
         catch (Exception ex)
         {
 
         }
 }

In general, please bear in mind that Invoke is an expensive operation, because the thread that executes the Invoke operation will block the UI thread and the windows messages will be stacked in the message queue.

I hope this information is useful.
Regards,
Peter
Telerik
RadChart for WinForms is obsolete. Now what?
Tags
GridView
Asked by
Ali
Top achievements
Rank 1
Answers by
Ali
Top achievements
Rank 1
Peter
Telerik team
Share this question
or