用 dataGridview  绑定数据,运行的时候,有时候出现很大的一个红叉叉,有时候又恨正常,报错说是dataGridview 在重绘的时候有空引用
我在网上查了一下,好想是说是线程安全引起的,不知道是不是,各位高手帮忙看看,急等。
private ProgressForm myProgressForm = null; //进度条
        private delegate bool ProcessIncreaseHandle(int nValue);
        private ProcessIncreaseHandle myIncrease = null;
 private void frmProductList_Load(object sender, EventArgs e)
        {
            try
            {
                //创建线程,加载产品
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(BeginStart));
                Control.CheckForIllegalCrossThreadCalls = false;
                thread.Start();                this.lblPageSize.Text = "1";
            }
            catch (Exception ex)
            {
                Program.log.Error(ex);
                MessageBox.Show(ex.Message);                if (!myProgressForm.IsDisposed)
                    myProgressForm.Close();            }        } /// <summary>
        ///查询分页列表,并绑定分页
        /// </summary>
        private void BeginStart()
        {            GetProductList(txtProductName.Text.Trim(), txtProductCode.Text.Trim(), txtShortName.Text.Trim(), pageSize);            GetPageCount();            this.lblPageCount.Text = pageCount.ToString();
        }/// <summary>
        /// 获取可销售产品列表
        /// </summary>
        /// <param name="name">产品名称</param>
        /// <param name="code">产品代码</param>
        /// <param name="shortName">险种名称</param>
        /// <param name="pageNum">页数</param>
        private void GetProductList(string name, string code, string shortName, int pageNum)
        {
            try
            {                DateTime dt1 = DateTime.Now;
                CSGetProductsResponse productsResponse = WebServersManage.ProductListWebService.GetProductList(
                                                            name, code, shortName, pageNum - 1);                if (productsResponse.soapProducts == null)
                {
                    dgProductList.DataSource = null;
                    pageCount = 1;
                    pageSize = 1;
                    this.lblAllCount.Text = "0";
                    return;                }                productInfoList = productsResponse.soapProducts;                string strGetListTime = string.Format("获取产品列表耗时:{0}", DateTime.Now - dt1);
                Program.log.Error(strGetListTime);                this.lblAllCount.Text = productsResponse.resultCount.ToString() == "" ? "0" : productsResponse.resultCount.ToString();                dgProductList.DataSource = productInfoList;
                #region 绑定代码                for (int i = 0; i < dgProductList.ColumnCount; i++)
                {
                    dgProductList.Columns[i].Visible = false;                }                dgProductList.Columns["productCode"].DisplayIndex = 0;
                dgProductList.Columns["productName"].DisplayIndex = 1;
                dgProductList.Columns["className"].DisplayIndex = 2;
                dgProductList.Columns["saleBeginDate"].DisplayIndex = 3;
                dgProductList.Columns["SaleEndDate"].DisplayIndex = 4;
                dgProductList.Columns["commitType"].DisplayIndex = 5;
                dgProductList.Columns["productCode"].HeaderText = "产品代码";
                dgProductList.Columns["productName"].HeaderText = "产品名称";
                dgProductList.Columns["className"].HeaderText = "险种名称";
                dgProductList.Columns["saleBeginDate"].HeaderText = "可销售起期";
                dgProductList.Columns["SaleEndDate"].HeaderText = "可销售止期";
                dgProductList.Columns["commitType"].HeaderText = "是否异步承保";                dgProductList.Columns["productCode"].Visible = true;
                dgProductList.Columns["productName"].Visible = true;
                dgProductList.Columns["className"].Visible = true;
                dgProductList.Columns["saleBeginDate"].Visible = true;
                dgProductList.Columns["SaleEndDate"].Visible = true;
                dgProductList.Columns["commitType"].Visible = true;                #endregion                if (dgProductList.Rows.Count > 0)
                {
                    for (int i = 0; i < this.dgProductList.Rows.Count; i++)
                    {
                        if (dgProductList["commitType", i].Value == null)
                        {
                            dgProductList["commitType", i].Value = "否";
                        }
                        else if(dgProductList["commitType", i].Value.ToString() == "0")
                        {
                            dgProductList["commitType", i].Value = "否";
                        }
                        else
                        {
                            dgProductList["commitType", i].Value = "是";
                        }
                    }
                }                CheckProduct();            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

解决方案 »

  1.   

    BeginStart()
    这个在哪应用过没贴出来
      

  2.   

                   System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(BeginStart));
    因为调用的是其他服务器的接口,有的时候因为网络的原因,查询的时候速度有点慢,所以在查询的时候用线材调用了一个进度条,CSGetProductsResponse productsResponse = WebServersManage.ProductListWebService.GetProductList( name, code, shortName, pageNum - 1); //调用接口private void ShowProcessBar()
            {
                myProgressForm = new ProgressForm();
                myIncrease = new ProcessIncreaseHandle(myProgressForm.ProcessIncrease);
                myProgressForm.StartPosition = FormStartPosition.CenterParent;
                myProgressForm.ShowDialog();
                myProgressForm = null;
            }
      

  3.   

    自己刚刚测试了一下,把线程关了,还是会出现那个红叉,
    报错信息 See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Windows.Forms.DataGridViewColumnCollection.GetNextColumn(DataGridViewColumn dataGridViewColumnStart, DataGridViewElementStates includeFilter, DataGridViewElementStates excludeFilter)
       at System.Windows.Forms.DataGridView.PaintColumnHeaders(Graphics g, Rectangle clipBounds, Boolean singleBorderAdded)
       at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
       at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.DataGridView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    lywx
        Assembly Version: 1.0.0.10
        Win32 Version: 1.0.0.10
        CodeBase: file:///C:/Program%20Files/cpiclywx/lywx.exe
    ----------------------------------------
    log4net
        Assembly Version: 1.2.10.0
        Win32 Version: 1.2.10.0
        CodeBase: file:///C:/Program%20Files/cpiclywx/log4net.DLL
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    IrisSkin2
        Assembly Version: 2006.3.22.45
        Win32 Version: 2006.3.22.45
        CodeBase: file:///C:/Program%20Files/cpiclywx/IrisSkin2.DLL
    ----------------------------------------
    System.Data
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    System.Web.Services
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Web.Services/2.0.0.0__b03f5f7f11d50a3a/System.Web.Services.dll
    ----------------------------------------
    fjsxxgrd
        Assembly Version: 1.0.0.10
        Win32 Version: 2.0.50727.42 (RTM.050727-4200)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.For example:<configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.