Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / March 2008

Tip: Looking for answers? Try searching our database.

DataGridView bound to object: Operation is not valid due to the current state of the object.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stuart Carnie - 27 Jul 2006 19:29 GMT
I am receiving this error as per the subject when binding a DataGridView to a collection of objects, where the collection is
BindingList<T>.

This post - http://tinyurl.com/hy4fg - details the steps to reproduce the error, and as noted you must start with an empty list
and then add a row, tab to the next field and then hit escape.  This will raise the error every time.

Any word or resolution for this?

I'm thinking I'll change my datagrid to not automatically add new, but create a button.

Cheers,

Stu
Linda Liu [MSFT] - 28 Jul 2006 08:50 GMT
Hi Stu,

Thank you for posting.

I performed a test based on your sample code and did see the problem you
said. I have looked up in our inner database and found a report about this
problem in it. It's a known bug in Visual Studio 8.0.50727.42. This bug
will be fixed in the next release of Visual Studio.

A probably workaround is to create a new class inherited from DataGridView
and override the OnKeyDown method. The following is a sample for you.

class MyDataGridView : DataGridView
   {
       protected override void OnKeyDown(KeyEventArgs e)
       {
            // the keyvalue of Escape key is 27
           if (e.KeyValue == 27)
           {
               if (this.CurrentCell.RowIndex > 0)
               {
                   base.OnKeyDown(e);
               }
           }
           else
           {
               base.OnKeyDown(e);
           }          
       }        
   }

Thus, when you edit the first row and tab to the second column and press
Escape, nothing happens. However, the function of Escape is still available
on other rows.

Thank you for reporting this bug to us. If you have anything unclear about
the above workaround, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Stuart Carnie - 28 Jul 2006 21:03 GMT
Linda, thanks for the reply and workaround, that will do the trick.

Cheers,

Stu

> Hi Stu,
>
[quoted text clipped - 57 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
semprini.luca@gmail.com - 11 Aug 2006 15:59 GMT
Hi, we tried the trick, but it works only if you press the "escape"
button.
If you try to go to a new row, then back to the first one and again to
the new added row (just switch between them), you'll get the same error
again (thus without pressing any key on the keyboard).
We tried to solve the problem, but couldn't find a way out yet. If
anybody has an idea....

Thanks, bye.

Luke

Stuart Carnie ha scritto:

> Linda, thanks for the reply and workaround, that will do the trick.
>
[quoted text clipped - 63 lines]
> >
> > This posting is provided "AS IS" with no warranties, and confers no rights.
semprini.luca@gmail.com - 12 Aug 2006 12:38 GMT
After some test I've found this "basic" solution, on the DataGridView
CellClick event check in the current row is a new row, and in case put
on che current cell a Space key and then a Backspace key. No more
trouble you will find.
I haven't my code here because I'm at home now, not at work, if someone
want see it I will paste here an example...
Note: to send "space" and "backspace" keys use SendKeys.Send("....");
function

bye, Luke

> Hi, we tried the trick, but it works only if you press the "escape"
> button.
[quoted text clipped - 77 lines]
> > >
> > > This posting is provided "AS IS" with no warranties, and confers no rights.
luca semprini - 17 Oct 2006 09:30 GMT
Hi, maybe we've found the final solution:
you must put the following code into the event "AddingNew" of the binding
source object:

if (gridTicketRows.Rows.Count == rowsBindingSource.Count){
rowsBindingSource.RemoveAt(rowsBindingSource.Count - 1);
return;
}

gridTicketRows is name of DataGridView, rowsBindingSource is name of
BindingSource.

good luck, :-)
Luke
bkwdesign - 24 Mar 2008 18:58 GMT
I wanted to echo thanks back to luca semprini  for her above code snippet which solved my woes for my DataGridView which was bound to a BindingSource. Before I found this blog, my WinForm was throwing "Operation is not valid.." errors if you tried to jump too much around my datagrid with a mouse click.

Now my users can do inline datagrid editing without fear!

Here's luca's code in VB.NET, where dgv is an instance of a DataGridView:

   Private Sub BindingSrcAddNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles bindingSrc.AddingNew
       If (Me.dgv.Rows.Count = Me.bindingSrc.Count) Then
           Me.bindingSrc.RemoveAt(Me.bindingSrc.Count - 1)
       End If
   End Su

From http://www.developmentnow.com/g/31_2006_7_0_0_795229/DataGridView-bound-to-objec
t-Operation-is-not-valid-due-to-the-current-state-of-the-object-.ht


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.