I am stuck with this problem of mine and it will be great help if someone solves this
problem for me
What I am trying to do is :
1) Intialize a DataTable datatable in form load event and assign its defaultview to a
datagridview dgvresult
2) On click of a button start an STA thread (I am actually working with WatIN IE hence the
need for making the thread STA ) which calls a method which creates an identical DataTable
dt as datatable created in step 1 and then adds 300 rows to this datatable.
3) calls a delegate which merges this dt with datatable and thus updates the dgvresult Well here is my problem :
Here is the code Snippet for the steps I just described :
static class Program
{
/// <summary>
/// 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
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
///frmmain code
/// <summary>
/// Delegate which Binds updated Datatable to gridview
/// </summary>
/// <param name="dt">The Datatable to be merged with main datatable</param>
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
/// <summary>
/// function of GenerateWords thread
/// </summary>
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();
}
}
}
Now the problem arises when I have many rows in datagridview(lets assume 1000)
Now if in UI I dont do anything with datagridview this code works fine without errors
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 )
Here is the error :-- When I run the exe I get this :-
Unhandled exception has occured in your application.
Object reference not set to an 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)
I checked it through the VS , It gives this error in Program.cs in this line :- Application.Run(new frmMain())
;
How to get around this problem?
Any help will be appreciated. Thank You :)