我在webform下做了一个工具,用于检测数据是否一致。由于数据量大,检测的过程我使用了BackgroundWorker多线程对数据一条条进行匹配,如果数据不一致,将通过datagridview的一列提示。检测流程没问题,但是在datagridview重新描绘后没有立刻表现,而是要先点击一下才出现,请问如何解决这个问题。代码如下:
        public MainForm()
        {
            InitializeComponent();
            this.tsrMainLblProgress.Text = "请加载数据!";
            this.tsrMainLblProgress.Image = Properties.Resources.tips;
            bwMatch.WorkerReportsProgress = true;
            bwMatch.WorkerSupportsCancellation = true;
            bwMatch.DoWork += new DoWorkEventHandler(match_DoWork);
            bwMatch.ProgressChanged += new ProgressChangedEventHandler(match_ProgressChanged);
            bwMatch.RunWorkerCompleted += new RunWorkerCompletedEventHandler(match_RunWorkerCompleted);
            
        }        /// <summary>
        /// 绑定本地数据
        /// </summary>
        protected void BindData()
        {
            try
            {
                this.dgrdData.DataSource = match.GetData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        /// <summary>
        /// 加载按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsrMainBtnOepn_Click(object sender, EventArgs e)
        {
            this.BindData();
            this.tsrMainLblProgress.Text = "数据载入完成!";
            this.tsrMainLblProgress.Image = Properties.Resources.success;
        }        /// <summary>
        /// 开始检测
        /// </summary>
        private void tsrMainBtnStart_Click(object sender, EventArgs e)
        {
            if (!bwMatch.IsBusy)
            {
                bwMatch.RunWorkerAsync();
            }
        }        /// <summary>
        /// 停止检测
        /// </summary>
        private void tsrMainBtnStop_Click(object sender, EventArgs e)
        {
            if (bwMatch.WorkerSupportsCancellation)
            {
                bwMatch.CancelAsync();
            }
        }        /// <summary>
        /// 检测操作
        /// </summary>
        private void match_DoWork(object sender, DoWorkEventArgs e)
        {
            this.tsrMainLblProgress.Text = "开始匹配……";
            this.tsrMainLblProgress.Image = Properties.Resources.loadding;
            BackgroundWorker worker = sender as BackgroundWorker;
            int i = 0;
            foreach (DataRow _dr in this.dt.Rows)
            {
                i++;
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    //拉取接口时间
                    _dr["Server"] = match.GetDomainWhois((string)_dr["ID"]).ToString();
                    _dr["Ed"] = "1";
                    worker.ReportProgress(i);
                    Thread.Sleep(500);
                }
            }
        }        /// <summary>
        /// 完成操作
        /// </summary>
        private void match_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.tsrMainLblProgress.Text = "已停止!";
                this.tsrMainLblProgress.Image = Properties.Resources.success_tips;
            }            else if (!(e.Error == null))
            {
                this.tsrMainLblProgress.Text = ("出错:" + e.Error.Message);
                this.tsrMainLblProgress.Image = Properties.Resources.warning;
            }            else
            {
                this.tsrMainLblProgress.Text = "匹配完成!";
                this.tsrMainLblProgress.Image = Properties.Resources.success;
            }
        }        private void match_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.tsrMainLblProgress.Text = ("匹配中(" + Convert.ToDecimal((double)e.ProgressPercentage * 100 / dt.Rows.Count).ToString("N2") + "%)……");
            //this.dgrdData.Refresh();
        }        /// <summary>
        /// 描绘
        /// </summary>
        private void dgrdData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3
                && ((string)this.dgrdData.Rows[e.RowIndex].Cells["Ed"].Value).Equals("1"))
            {                string strLocal = Convert.ToString(this.dgrdData.Rows[e.RowIndex].Cells["ExpireLocal"].Value);
                string strServer = Convert.ToString(this.dgrdData.Rows[e.RowIndex].Cells["Server"].Value);
                string strStatus = string.Empty;
                Image img;
                Color cor = e.CellStyle.BackColor;
                if (string.IsNullOrEmpty((string)this.dgrdData.Rows[e.RowIndex].Cells[2].Value))
                {
                    strStatus = (string)this.dgrdData.Rows[e.RowIndex].Cells[3].Value;
                    img = Properties.Resources.tips;
                    cor = Color.Yellow;
                }
                else if (!((string)this.dgrdData.Rows[e.RowIndex].Cells[1].Value).Equals((string)this.dgrdData.Rows[e.RowIndex].Cells[2].Value))
                {
                    strStatus = "不匹配";
                    img = Properties.Resources.warning;
                    cor = Color.Red;
                }
                else
                {
                    strStatus = "匹配成功";
                    img = Properties.Resources.success;
                }
                Rectangle newRect = new Rectangle(e.CellBounds.X + 3, e.CellBounds.Y + 2, 16, 16);                using (Brush gridBrush = new SolidBrush(this.dgrdData.GridColor), backColorBrush = new SolidBrush(cor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush, 2))
                    {
                        // Erase the cell.
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);                        //划线
                        Point p1 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top);
                        Point p2 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top + e.CellBounds.Height);
                        Point p3 = new Point(e.CellBounds.Left, e.CellBounds.Top + e.CellBounds.Height);
                        Point[] ps = new Point[] { p1, p2, p3 };
                        e.Graphics.DrawLines(gridLinePen, ps);                        //画图标
                        e.Graphics.DrawImage(img, newRect);
                        //画字符串
                        e.Graphics.DrawString(strStatus, e.CellStyle.Font, Brushes.Black,
                            e.CellBounds.Left + 20, e.CellBounds.Top + 3, StringFormat.GenericDefault);
                        e.Handled = true;
                    }
                }
            }
        }