Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1290
Threading + WinGrid
posted

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)

            {                

                ugConnections.Rows[rowIndex].Cells["Status"].Value = "Failed";

            }

        }

 

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: 

       ugConnections.Rows[rowIndex].Cells["Status"].Value = "Failed";

 

OR

 

Value property can not be assigned while BeforeCellUpdate event is in progress 

on this line:


 ugConnections.Rows[rowIndex].Cells["Status"].Value = "Connecting...";

 

Where did I go wrong?

Parents
No Data
Reply
  • 69832
    Suggested Answer
    Offline posted

    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.

Children