When I have a gridview bound to a bindinglist and I update the data that the list is bound to in a seperate thread (than the GUI thread) I am getting crossthreading errors in the assignment statement. I am attempting to get around the problem by alerting the main thread to update the value. Is this the way that the gridview binding is intended to operate or have I left something out?
8 Answers, 1 is accepted
0

Richard Slade
Top achievements
Rank 2
answered on 11 Feb 2011, 06:31 PM
Hello Brian,
Cross threading, or updating the UI from a thread that did not create it is not something one can do in .NET. You'll need to call invoke to be able to update the UI. Please have a look at this MSDN Blog article on using invoke.
Hope that helps
Richard
Cross threading, or updating the UI from a thread that did not create it is not something one can do in .NET. You'll need to call invoke to be able to update the UI. Please have a look at this MSDN Blog article on using invoke.
Hope that helps
Richard
0

Brian
Top achievements
Rank 1
answered on 11 Feb 2011, 07:19 PM
I am updating a bindinglist that is not declared in the UI. Even if I invoke the call to update the bindinglist, I still get the crossthreading error
0
Accepted

Richard Slade
Top achievements
Rank 2
answered on 11 Feb 2011, 07:49 PM
Hello Brian,
Below is a small sample to try. I cannot get this to produce a cross threading error. Let me know how you get on with it.
Designer File
Form1.cs
regards,
Richard
Below is a small sample to try. I cannot get this to produce a cross threading error. Let me know how you get on with it.
Designer File
namespace
RadGridView_BindingListBackground_CS
{
partial
class
Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void
InitializeComponent()
{
this
.radGridView1 =
new
Telerik.WinControls.UI.RadGridView();
this
.backgroundWorker1 =
new
System.ComponentModel.BackgroundWorker();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).BeginInit();
this
.SuspendLayout();
//
// radGridView1
//
this
.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this
.radGridView1.Location =
new
System.Drawing.Point(0, 0);
this
.radGridView1.Name =
"radGridView1"
;
this
.radGridView1.Size =
new
System.Drawing.Size(284, 262);
this
.radGridView1.TabIndex = 0;
this
.radGridView1.Text =
"radGridView1"
;
//
// backgroundWorker1
//
this
.backgroundWorker1.DoWork +=
new
System.ComponentModel.DoWorkEventHandler(
this
.backgroundWorker1_DoWork);
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(284, 262);
this
.Controls.Add(
this
.radGridView1);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
this
.FormClosing +=
new
System.Windows.Forms.FormClosingEventHandler(
this
.Form1_FormClosing);
this
.Load +=
new
System.EventHandler(
this
.Form1_Load);
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).EndInit();
this
.ResumeLayout(
false
);
}
#endregion
private
Telerik.WinControls.UI.RadGridView radGridView1;
private
System.ComponentModel.BackgroundWorker backgroundWorker1;
}
}
Form1.cs
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
RadGridView_BindingListBackground_CS
{
public
partial
class
Form1 : Form
{
private
Boolean m_Continue;
private
BindingList<MyObject> m_List =
new
BindingList<MyObject>();
private
delegate
void
ResetBindingCallback();
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
m_Continue =
true
;
m_List.Add(
new
MyObject(1,
"Name 1"
));
m_List.Add(
new
MyObject(2,
"Name 2"
));
m_List.Add(
new
MyObject(3,
"Name 3"
));
this
.radGridView1.AutoGenerateColumns =
true
;
this
.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
this
.radGridView1.ReadOnly =
true
;
this
.radGridView1.DataSource = m_List;
this
.backgroundWorker1.RunWorkerAsync();
}
private
void
backgroundWorker1_DoWork(
object
sender, DoWorkEventArgs e)
{
while
(m_Continue ==
true
)
{
m_List[0].Id = m_List[0].Id + 1;
m_List[0].Name =
"Name "
+ m_List[0].Id;
m_List[1].Id = m_List[1].Id + 1;
m_List[1].Name =
"Name "
+ m_List[1].Id;
m_List[2].Id = m_List[2].Id + 1;
m_List[2].Name =
"Name "
+ m_List[2].Id;
Invoke(
new
ResetBindingCallback(ResetBinding));
System.Threading.Thread.Sleep(1000);
}
}
private
void
ResetBinding()
{
if
(m_Continue) { m_List.ResetBindings(); }
}
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
{
m_Continue =
false
;
}
}
public
class
MyObject
{
public
MyObject() { }
public
MyObject(
int
id,
string
name)
{
this
.Id = id;
this
.Name = name;
}
public
int
Id
{
get
;
set
; }
public
string
Name
{
get
;
set
; }
}
}
regards,
Richard
0

Richard Slade
Top achievements
Rank 2
answered on 14 Feb 2011, 11:33 AM
Hello,
Did this help? If so please remember to mark as answer. If you need further assistance, let me know
thanks
Richard
Did this help? If so please remember to mark as answer. If you need further assistance, let me know
thanks
Richard
0

Brian
Top achievements
Rank 1
answered on 17 Feb 2011, 05:08 PM
I updated Telerik and now everything as far as the crossthreading goes,is working.
The data that is populating the grid gets updated with no error.
The problem now is that the grid does not reflect these changes until I click to sort or something of that nature on the grid.
The data that is populating the grid gets updated with no error.
The problem now is that the grid does not reflect these changes until I click to sort or something of that nature on the grid.
0
Accepted

Richard Slade
Top achievements
Rank 2
answered on 17 Feb 2011, 05:11 PM
Hi Brian,
Glad that this is almost sorted for you. You may want to try calling
at the end. this should bring everything up to date.
Richard
Glad that this is almost sorted for you. You may want to try calling
this
.radGridView1.MasterTemplate.Refresh();
Richard
0

Brian
Top achievements
Rank 1
answered on 17 Feb 2011, 05:22 PM
I think that did it.
0

Richard Slade
Top achievements
Rank 2
answered on 17 Feb 2011, 05:28 PM
Great. Glad I could help
All the best
Richard
All the best
Richard