Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

817 views
Skip to first unread message

Stuart Carnie

unread,
Jul 27, 2006, 2:29:55 PM7/27/06
to
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]

unread,
Jul 28, 2006, 3:50:04 AM7/28/06
to
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

unread,
Jul 28, 2006, 4:03:35 PM7/28/06
to
Linda, thanks for the reply and workaround, that will do the trick.

Cheers,

Stu

sempri...@gmail.com

unread,
Aug 11, 2006, 10:59:28 AM8/11/06
to
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:

sempri...@gmail.com

unread,
Aug 12, 2006, 7:38:34 AM8/12/06
to
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

luca semprini

unread,
Oct 17, 2006, 4:30:43 AM10/17/06
to
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

luke

unread,
Oct 17, 2006, 4:40:04 AM10/17/06
to
I've read some users that got this problem.
Maybe we've found the final solution:

I haven't get error anymore!

good luck, :-)
Luke

bkwdesign

unread,
Mar 24, 2008, 1:58:50 PM3/24/08
to
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 Sub

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

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com

0 new messages