Solved

DataGridView problem with threads (Big Red Cross)

Posted on 2014-12-07
2
76 Views
Last Modified: 2016-06-23
hi,

i have a problem with the DataGridView when i fill it using a thread, sometimes it crashes and gives a Big Red Cross.
i bind it with a DataTable and fill the DataTable from the Database, and there is icon column in the DataGridView updated on CellFormatting event
i searched for this error online and found this solution:

using System;
using System.Windows.Forms;

namespace DataGridViewPlus
{
public class DataGridViewPlus : DataGridView
{
///
/// This prevents the "red X" error which happens when you cause updates to a datagrid
/// from multiple threads. It catches the OnPaint() exception and invalidates
/// the grid so it gets redrawn the next time the application hits its message loop.
///
/// The following solution to this problem was found
/// at http://social.msdn.microsoft.com/forums/en-US/winforms/thread/fdd94896-80e9-4e91-9ed5-0348bf2633a9
///
protected override void OnPaint( PaintEventArgs e )
{
try
{
base.OnPaint( e );
}
catch
{
Invalidate();
}
}
}
}


i tried it but gives me this error instead

"System.IndexOutOfRangeException: index 0 does not have a value"

and this is my code:

        If Not (UpdateThread Is Nothing) Then
            If UpdateThread.IsAlive = True Then
                Exit Sub
            End If
        End If
        If UpdateFlagOrdersDGV = False Then
            UpdateFlagOrdersDGV = True
            UpdateThread = New Thread(UpdateThreadStart)
            UpdateThread.IsBackground = True
            UpdateThread.Name = "UpdateThread"
            UpdateThread.Start()
            UpdateFlagOrdersDGV = False
        End If

thank you
0
Comment
Question by:ayran1805
2 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 40485673
I had a similar problem with a thread-updated grid and the only way I got it to stop giving me errors was to do something like this

1. Use a lock over the whole database load procedure. grids are not inherently thread safe.
2. Load the data into a temporary datatable
3. merge the data tables
4. release the lock
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video teaches viewers about errors in exception handling.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question