?
Solved

Datagridview control error - InvalidoperationException was unhandled when clearing data in C Sharp?

Posted on 2011-05-13
10
Medium Priority
?
2,027 Views
Last Modified: 2012-06-27
Please note image and code where the following Invalidoperationexception error occurs:
Rows collection cannot be programmatically cleared when the DataGridView control is data-bound to anything else than an IBindingList that supports change notification and allows deletion.
at line item: dgvLogger.Rows.Clear();

All I'm doing is after one minute's time, take my datagrid on a form and clear its records to reinsert logging information from SQL Server.  How may I change the syntax to allow for the rows to be clear in the datagrid?

I understand that according to the following URL -
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/b90db7da-38b9-439b-94bd-2844ae287d6c

... I may need to use BindingList instead of ArrayList but having trouble with what that means?  Any translation is much appreciated!

This question is a continuance from another question...http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27020663.html#a35754950
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;

namespace Logger
{
    public partial class frmMain : Form
    {

        Thread SQLDataCheckerThread;


        public frmMain()
        {
            InitializeComponent();

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            VisualData();

            this.FormClosed += new FormClosedEventHandler(frmMain_FormClosed);
            SQLDataCheckerThread = new Thread(new ThreadStart(CheckForNewData));
            SQLDataCheckerThread.Start();

        }

        void CheckForNewData()
        {
            while (true)
            {

                Thread.Sleep(60000);

                if (People_Logged_In() == true)
                {
                    
                    {

                        dgvLogger.Rows.Clear();
                        VisualData();
                        Form.ActiveForm.Visible = true;
                    }
                }
                else
                {
                    
                //        Form.ActiveForm.Visible = false;
                
                }

            }
        }

Open in new window

ee-error-datagrid.png
0
Comment
Question by:stephenlecomptejr
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 35755256
Whenever one needs to interact with WinForms UI from within another thread (the one that didn't create the UI controls) one has to rely on the Control.Invoke (or BeginInvoke/EndInvoke pair) method. Otherwise one gets an InvalidOperationException exception with message “Cross-thread operation not valid: Control ‘name here’ accessed from a thread other than the thread it was created on”. This happens because WinForms (and .net framework in general) isn’t thread safe.

use Control.Invoke
0
 
LVL 8

Expert Comment

by:databoks
ID: 35757736
Dj_Alik i don't belive that the problem is a Cross Thread Exception.

Author: Do you use a DataSource? If so try set the DgvLogger.DataSource = null;

If that gives an error enuramte through all the rows like this:

foreach(DataRow row in DgvLogger.Rows)
{
    DgvLogger.Rows.RemoveAt(Row.Index);

}

I guess you are using the built-in DataGrid.

The Enumuration and delete the rows through a loop is not the best solution.

This takes to much time.

I would just want to see if that succeds.

0
 
LVL 8

Expert Comment

by:databoks
ID: 35757855
When i am thinking, maybe dj_alik is right.

Do you remember the last sample i gave you with the Textbox?

TextBox1.BeginInvoke(Method....)....

You have to do the same here:

dgvLogger.BeginInvoke(MethodInvoker).

Instead of textbox, then just the gridview.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 35759911
When I try the following I still get an error...
ee-datagrid.png
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35759923
How can I update my user interface from a thread that did not create it
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx
0
 
LVL 8

Expert Comment

by:databoks
ID: 35760406
Here is the code:



DgvLogger.BeginInvoke((MethodInvoker)delegate()
                {

                     DgvLogger.Rows.Clear(); 
                       
                });

Open in new window

0
 
LVL 8

Accepted Solution

by:
databoks earned 2000 total points
ID: 35760435
I have seen your project and i have updated it:

https://filedb.experts-exchange.com/incoming/ee-stuff/8098-Logger.zip 
0
 
LVL 8

Expert Comment

by:databoks
ID: 35760447
You also may use Try Catch to provide good error handling to your users.


Im sorry that i misunderstood you before..

I hope this will help you.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 35762522
Thanks for taking the time to update the project.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

864 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