Solved

DataGridView problem with threads (Big Red Cross)

Posted on 2014-12-07
2
56 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now