Aggiornamento di DataGridView tramite un thread durante lo scorrimento

Sono bloccato con questo mio problema e sarà di grande aiuto se qualcuno risolve questo problema

problema per me

Quello che sto cercando di fare è:

1) Intializzare un DataTable datatable in form load event e assegnare la sua defaultview a a

datagridview dgvresult

2) Al clic di un pulsante avvia un thread STA (sto effettivamente lavorando con WatIn IE da cui il

necessario per rendere il thread STA) che chiama un metodo che crea un DataTable identico

dt come datatable creato nel passaggio 1 e quindi aggiunge 300 righe a questo datatable.

3) chiama un delegato che fonde questo dt con datatable e aggiorna quindi il dgvresult Bene ecco il mio problema:

Ecco il codice Snippet per i passaggi che ho appena descritto:

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()); } } 

/// codice 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(); } } } 

Ora il problema sorge quando ho molte righe in datagridview (assumiamo 1000)

Ora se in UI non faccio nulla con datagridview questo codice funziona bene senza errori

Ma se continuo a scorrere il datgridview quando viene chiamato il metodo bind, l’applicazione dà un errore e termina semplicemente a causa del suo errore nell’aggiornare il datgridview suppongo (o forse a causa di un errore nel richiamo della vista dati)

Ecco l’errore: – Quando corro l’exe ottengo questo: –

Si è verificata un’eccezione non gestita nell’applicazione.

Riferimento dell’object non impostato su un object.

 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) 

Ho controllato attraverso il VS, dà questo errore in Program.cs in questa riga: – Application.Run(new frmMain()) ;

Come aggirare questo problema?

Qualsiasi aiuto sarà apprezzato. Grazie 🙂