?
Solved

DataGridView problem with threads (Big Red Cross)

Posted on 2014-12-07
2
Medium Priority
?
119 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

777 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