Hi,
I am using Infragistics 9.1 and I have a WinGrid that basically displays a list of SQL connections and their status.
Within the InitializeRow event, I have this:
private void ugConnections_InitializeRow(object sender, InitializeRowEventArgs e)
{
string Server = e.Row.Cells["Server"].Value.ToString();
string Database = e.Row.Cells["Database"].Value.ToString();
string User = e.Row.Cells["User"].Value.ToString();
string Password = e.Row.Cells["Password"].Value.ToString();
ThreadStart ts = delegate() { ConnectionTest(Server, Database, User, Password, e.Row.Index); };
Thread t = new Thread(ts);
t.Start();
}
And this the method that is being threaded:
private void ConnectionTest(string Server, string Database, string User, string Password, int rowIndex)
ugConnections.Rows[rowIndex].Cells["Status"].Value = "Connecting...";
//removed code which concatenates all the parameters to a StringBuilder
SqlConnection conn = new SqlConnection(sb.ToString());
try
conn.Open();
if (conn.State == ConnectionState.Open)
ugConnections.Rows[rowIndex].Cells["Status"].Value = "Connected";
else
ugConnections.Rows[rowIndex].Cells["Status"].Value = "Failed";
catch (SqlException sex)
The code runs fine, but every once in a while, I'll get an error
Object reference not set to an instance of an object. on this line:
OR
Value property can not be assigned while BeforeCellUpdate event is in progress
on this line:
Where did I go wrong?
You can't set properties on the UltraGrid or its sub-objects in this manner because you are not marshalling the cross-thread calls properly. You would have to use the control's BeginInvoke method (to set the value on a cell) from your 'ConnectionTest' method because that method executes on a separate thread, and the grid's objects can only be accessed from the main UI thread.
There's a long, detailed discussion of threading with the WinGrid here. Basically, it is not possible to do safely do data binding across multiple threads.
Work with a dataset bound to a grid on a separate thread - Infragistics Community