grid 出现红叉

This issue is not connected with the XtraGrid directly. Unfortunately, you can't change the grid's data source in a background thread since it will cause a lot of problems with synchronization. The XtraGrid may perform some operations with the underlying data source at the same time as your background thread changes it. In this case the grid may receive a Change notification later and will try to update rows from the data source which will cause the mentioned problem. This problem may occur in a lot of cases. For example, when a user edits data, groups it or the XtraGrid tries to recalculate the summaries. The only solution to this problem is to change the Grid's DataSource reference within a background thread (NOTE: You will need to implement it using the Invoke method). Said differently, within a background thread you should work with a local copy of the DataSource and pass its clone to the Grid's DataSource when necessary. In the attached example you will find a sample project which demonstrates this approach.

https://www.devexpress.com/Support/Center/Question/Details/AK2981/can-i-avoid-the-argumentoutofrangeexception-when-updating-the-grid-s-data


根据上面的解释,修改代码:

定义临时变量 dataTable dt

将DGV的datasource赋值给DT,数据导入时,删除已导入的DT行。任务完成后,DGV显示DT

发现还是会莫名的报错,干脆在赋值后,再把DGV的datasource设置为NULL,虽然有点难看,但终于是不报错了

//提交
        private void btnSumit_Click(object sender, EventArgs e)
        {
            try
            {
                dt = dgv.DataSource as DataTable;                
                if (dt == null || dt.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据!", "提示");
                    return;
                }
                if (cmbVendor.SelectedValue == null || cmbVendor.SelectedIndex < 0)
                {
                    MessageBox.Show("请选择供应商!", "提示");
                    cmbVendor.Select();
                    return;
                }
                vendorId = Convert.ToInt32(cmbVendor.SelectedValue);
                cmbVendor.Enabled = false;
                dgv.DataSource = null;
                this.backgroundWorker1.RunWorkerAsync(); // 运行 backgroundWorker 组件
                FormProgressBar form = new FormProgressBar(this.backgroundWorker1);// 显示进度条窗体
                form.ShowDialog(this);
                form.Close();
                //this.dgv.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

  

 

 


 

以下异常的:

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
   在 System.Windows.Forms.DataGridViewCell.GetEditedFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewDataErrorContexts context)
   在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.DataGridView.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

posted on 2017-07-07 21:42  xiaoshu7  阅读(497)  评论(1编辑  收藏  举报

导航