Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGridView problem with threads (Big Red Cross)

Posted on 2014-12-07
2
Medium Priority
?
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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…
Suggested Courses

610 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