Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating DataGridView via a thread when scrolling
    primarykey
    data
    text
    <p>I am stuck with this problem of mine and it will be great help if someone solves this </p> <p>problem for me</p> <p>What I am trying to do is :</p> <p>1) Intialize a DataTable datatable in form load event and assign its defaultview to a </p> <p>datagridview dgvresult</p> <p>2) On click of a button start an STA thread (I am actually working with WatIN IE hence the </p> <p>need for making the thread STA ) which calls a method which creates an identical DataTable </p> <p>dt as datatable created in step 1 and then adds 300 rows to this datatable.</p> <p>3) calls a delegate which merges this dt with datatable and thus updates the dgvresult Well here is my problem :</p> <p>Here is the code Snippet for the steps I just described :</p> <pre><code>static class Program { /// &lt;summary&gt; /// 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 /// &lt;/summary&gt; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } } </code></pre> <p>///frmmain code</p> <pre><code> /// &lt;summary&gt; /// Delegate which Binds updated Datatable to gridview /// &lt;/summary&gt; /// &lt;param name="dt"&gt;The Datatable to be merged with main datatable&lt;/param&gt; 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 &gt; 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 /// &lt;summary&gt; /// function of GenerateWords thread /// &lt;/summary&gt; 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 &lt; 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(); } } } </code></pre> <p>Now the problem arises when I have many rows in datagridview(lets assume 1000)</p> <p>Now if in UI I dont do anything with datagridview this code works fine without errors</p> <p>But if I keep scrolling the datgridview when the bind method is called the application gives error and just terminates due to its failure to update the datgridview I suppose ( or maybe due to failure in invoking the datagridview )</p> <p>Here is the error :-- When I run the exe I get this :-</p> <p>Unhandled exception has occured in your application.</p> <p>Object reference not set to an object.</p> <pre><code>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&amp; m) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.DataGridView.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) </code></pre> <p>I checked it through the VS , It gives this error in Program.cs in this line :- <code>Application.Run(new frmMain())</code>;</p> <p>How to get around this problem?</p> <p>Any help will be appreciated. Thank You :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload