Setting grid datasource within thread while table(source) is being filled...

V

VM

I'm trying to dynamically display the rows in a datagrid while filling the
datatable source. I'm doing it this way because we had already finished the
app but this section sometimes took so long that we had to add
multithreading. I do it with this code (this code is not in the class that
generates the form and the controls):

public DataTable OpenAuditAZMFileToView(string sFileName, ref DataGrid
dataGrid_auditAddress)
{
DataRow rowAudit;
sAuditRecord = sr.ReadLine();
while (sAuditRecord != null) //this runs 35,000 times. And the error
occurs with big files.
{
rowAudit = table_Audit.NewRow();
... //code Fills the row with appropriate data
table_Audit.Rows.Add (rowAudit);
SetGridDataSourceHandler handler = new
SetGridDataSourceHandler(this.SetGridDataSource);
_dataGrid_auditAddress.Invoke(handler, new object[]{DT_Audit});
sAuditRecord = sr.ReadLine();
}
}
private delegate void SetGridDataSourceHandler(object dataSource);
private void SetGridDataSource(object dataSource)
{
if (_dataGrid_auditAddress.InvokeRequired)
{
SetGridDataSourceHandler handler = new
SetGridDataSourceHandler(this.SetGridDataSource);
_dataGrid_auditAddress.Invoke(handler, new object[]{table_Audit});
}
else
{
this._dataGrid_auditAddress.DataSource = dataSource;
}
}

If I run the Invoke once after the table's been filled, it'll work fine. But
it I do it like this (set the grid source many times while the table's being
filled), I get different types of errors during runtime, such as "Object
reference not set to an instance of an object". And it only happens with
huge files (in my case, 35,000 lines).

Any help will be appreciated.
 
C

Chad Z. Hower aka Kudzu

table's being filled), I get different types of errors during runtime,
such as "Object reference not set to an instance of an object". And it
only happens with huge files (in my case, 35,000 lines).

This is basically .NET's version of an Access Violation. You need to narrow
it down to which line of code this is occurring on and then find out either
why the variable is nil, or the object it was using has been destroyed.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
V

VM

But I don't think that's it because the errors show up when I'm dynamically
setting the datasource of the datagrid while the source (table) is being
filled. If the loop runs 35,000 times I'll be -adding row to table, setting
grid datasource- 35,000 times, and that's where I encounter the problem. If
I do it once after the whole table has been filled, it'll work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top