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?
Oops, sorry about that. I fixed the link.
Binding the grid (or any control) to a data source that is on another thread is fraught with danger. Neither the grid nor the BindingManager in DotNet are thread-safe. So there will always be a chance that the grid or the BindingManager will request data from the data source while it is being modified on the other thread, and this will cause your application to get out of synch which may result in Exceptions or data corruption. And any such exceptions will be nearly impossible to track down, because they won't happen until after the operations that really caused them are long-since completed.
The hyperlink isn't working.
So, are you saying it's not possible to accomplish what I want? Which is to display connection status of SQL servers in a WinGrid in a threaded fashion. Because if threads aren't used, the main thread would just hang if trying to connect to a SQL server which is unresponsive.
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
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.