滚动时通过线程更新DataGridView

我遇到了我的这个问题,如果有人解决了这个问题,那将会很有帮助

我的问题

我想做的是:

1)在表单加载事件中初始化DataTable数据表并将其defaultview分配给a

datagridview dgvresult

2)点击一个按钮启动一个STA线程(我实际上正在使用WatIN IE,因此

需要使线程STA)调用一个创建相同DataTable的方法

dt作为在步骤1中创建的数据表,然后向此数据表添加300行。

3)调用一个委托,它将这个dt与datatable合并,从而更新dgvresult这里是我的问题:

这是我刚才描述的步骤的代码片段:

static class Program { ///  /// The main entry point for the application.This method is made STAThread as I need to intialize WatIN IE in the form load of frmMain ///  [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } } 

/// frmmain代码

  ///  /// Delegate which Binds updated Datatable to gridview ///  /// The Datatable to be merged with main datatable delegate void Bind_DataTable_to_GridView_Delegate(DataTable dt); private void bind_DataTable_to_GridView(DataTable dt) { if (dgvResult.InvokeRequired) { Bind_DataTable_to_GridView_Delegate del = new Bind_DataTable_to_GridView_Delegate(bind_DataTable_to_GridView); dgvResult.Invoke(del, new object[] { dt }); } else { datatable.Merge(dt); dgvResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //Autosizes the gridview foreach (DataGridViewColumn dgvcol in dgvResult.Columns) { dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable; } } } WatiN.Core.IE ie; private void frmMain_Load(object sender, EventArgs e) { //intialize WatIN IE ie = new IE(URLs.mainURL); //initialization of columns in datatable DataColumn datacolumn1 = new DataColumn("Words"); //Adding of columns in datatable datatable.Columns.Add(datacolumn1); //Making the datatable permanent datatable.AcceptChanges(); //Assigning default view of datatble as dgvResult's datasource dgvResult.DataSource = datatable.DefaultView; foreach (DataGridViewColumn dgvcol in dgvResult.Columns) { dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable; } } private void btnGenerateWords_Click(object sender, EventArgs e) { try { if (datatable.Rows.Count > 0) { //Initializes the GenerateWords Thread GenerateWords = new Thread(GenW); //Sets the apartment state to Static GenerateWords.SetApartmentState(ApartmentState.STA); //Starts the GenerateWords Thread GenerateWords.Start(); } } #endregion #region function of GenerateWords thread ///  /// function of GenerateWords thread ///  void GenW() { DataColumn datacolumn1 = new DataColumn("Words"); //Adding of columns in datatable DataTable dt = new DataTable(); dt.Columns.Add(datacolumn1); //At this point datatable has say 20 rows for (int dtindex = 0; dtindex < datatable.Rows.Count; dtindex++) { /*Code Which adds successfully 300 fresh rows to dt*/ //sends datasource of dgvresult as dt bind_DataTable_to_GridView(dt); dt.Clear(); } } } 

现在当我在datagridview中有很多行时出现问题(假设为1000)

现在,如果在UI中我没有对datagridview做任何事情,这段代码可以正常运行而不会出错

但是如果我在调用bind方法的时候继续滚动datgridview,那么应用程序会给出错误并因为我无法更新datgridview而终止(或者可能是由于调用datagridview失败)

这是错误: – 当我运行exe时,我得到这个: –

您的应用程序中发生了未处理的exception。

对象引用未设置为对象。

 System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint) at System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts) at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow) at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded) 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) 

我通过VS检查了它,它在这一行的Program.cs中给出了这个错误: – Application.Run(new frmMain()) ;

如何解决这个问题?

任何帮助将不胜感激。 谢谢 :)