Solved

DataGridView problem with threads (Big Red Cross)

Posted on 2014-12-07
2
85 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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