• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2153
  • Last Modified:

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

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
stephenlecomptejr
Asked:
stephenlecomptejr
  • 5
  • 3
  • 2
1 Solution
 
dj_alikCommented:
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
 
databoksCommented:
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
 
databoksCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
stephenlecomptejrAuthor Commented:
When I try the following I still get an error...
ee-datagrid.png
0
 
dj_alikCommented:
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
 
databoksCommented:
Here is the code:



DgvLogger.BeginInvoke((MethodInvoker)delegate()
                {

                     DgvLogger.Rows.Clear(); 
                       
                });

Open in new window

0
 
databoksCommented:
I have seen your project and i have updated it:

https://filedb.experts-exchange.com/incoming/ee-stuff/8098-Logger.zip 
0
 
databoksCommented:
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
 
stephenlecomptejrAuthor Commented:
Thanks for taking the time to update the project.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now