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

Binding to GridView

2 Answers 119 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Russ
Top achievements
Rank 1
Russ asked on 18 Jun 2009, 10:51 PM
I'm thinking of purchasing your controls for an upcoming project but I am getting a couple of errors that I'm hoping you can help me with.  I have a sample project available.

1) The error below happens when I bind to the grid:
System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Telerik.WinControls.UI.GridTableElement.GetRowStyleForType(String elementTypeFullName)
   at Telerik.WinControls.UI.GridTableElement.GetRowStyle(GridRowElement row)
   at Telerik.WinControls.UI.GridRowElement.GetRowStyle()
   at Telerik.WinControls.UI.GridRowElement.ApplyCustomFormatting()
   at Telerik.WinControls.UI.GridRowElement.UpdateInfo()
   at Telerik.WinControls.UI.GridTableBodyElement.UpdateState()
   at Telerik.WinControls.UI.GridTableElement.UpdateCurrentColumn()
   at Telerik.WinControls.UI.GridViewTemplate.SetCurrentColumn(GridViewColumn column, Boolean update, Boolean shift, Boolean control)
   at Telerik.WinControls.UI.GridViewTemplate.set_CurrentColumn(GridViewColumn value)
   at Telerik.WinControls.UI.GridTableElement.UpdateCurrentPosition()
   at Telerik.WinControls.UI.GridViewInfo.SetCurrentRow(GridViewRowInfo row, Boolean setPosition, Boolean shift, Boolean control, Boolean rightMouseButton)
   at Telerik.WinControls.UI.GridViewTemplate.SetCurrentRow(GridViewRowInfo rowInfo)
   at Telerik.WinControls.Data.DataAccessComponent.InitDataGrid()
   at Telerik.WinControls.Data.DataAccessComponent.Bind(Object dataSource, String dataMember)
   at Telerik.WinControls.Data.DataAccessComponent.set_DataSource(Object value)
   at WindowsApplication4.Form1.worker_DoWork(Object sender, DoWorkEventArgs e) in c:\devprojects\WindowsApplication4\WindowsApplication4\Form1.vb:line 14

2) The second error is Exception has been thrown by the target of an invocation. There is no clear way to reproduce. Sometimes it happens on startup, other times it me three or four clicks.
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(Message& m)
   at Telerik.WinControls.RadControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at WindowsApplication4.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Please le tme know if you can help.

2 Answers, 1 is accepted

Sort by
0
Russ
Top achievements
Rank 1
answered on 18 Jun 2009, 10:56 PM
Here's the jist of the code

 

 

  Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
    With Me.worker  
      .RunWorkerAsync()  
    End With 
  End Sub 
 
  Private Sub worker_DoWork(ByVal sender As ObjectByVal e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork  
    Try 
      Using db As New DataClasses1DataContext  
        db.Log = Console.Out  
        Dim q = From c In db.GetTable(Of candidate)() _  
                        Select c.candidate_num, c.first_name, c.last_name  
        Me.RadGridView1.DataSource = q  ''This throws error #1 from the original post.  I believe this is the same line causing error #2 but I'm unsure  
      End Using  
    Catch ex As Exception  
      Using sw As New System.IO.StreamWriter(My.Application.Info.DirectoryPath & "\LogFile.txt")  
        sw.WriteLine(ex.ToString)  
      End Using  
    End Try 
  End Sub 
  Private Sub Clear_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Clear.Click  
    Me.RadGridView1.DataSource = Nothing 
  End Sub 
 
  Private Sub Load_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles LoadData.Click  
    With Me.worker  
      .RunWorkerAsync()  
    End With 
  End Sub 

 

0
Victor
Telerik team
answered on 19 Jun 2009, 03:45 PM
Hello Russ,

Thank you for writing.
 
RadControls are not thread safe. You must employ locking constructs and synchronization logic if you need to access the grid from another thread. This is why exceptions get thrown.
 
I have attached a sample application we prepared for another customer to demonstrate a way of doing this. It is in C#, but you can use this converter to convert the project code to VB in case you are not familiar with C#. Write again if you have other questions about our controls.

Best wishes,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Russ
Top achievements
Rank 1
Answers by
Russ
Top achievements
Rank 1
Victor
Telerik team
Share this question
or