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.
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.