Problem with RowChanging event

Hi,

if have a DataSet (SQLExpress) whith 2 Tables.
On one table I add a RowChanging event handler.
When I add a record in a boud datagridview then an exception occures in the handler. (After changing the postion in the DGV the event is fired).
Here my handler:

void PCs_RowChanging(object sender,DataRowChangeEventArgs e) {
if (PCsBindingSource.IsBindingSuspended) {
return;
}
try {
MessageBox.Show(e.Action.ToString());
}
catch(Exception eX) {
MessageBox.Show(eX.Message);
}
}

I get and "Index out of range" exception and my DGV is a red rectangle with red lines across it.
After the exception I can see my MessageBox telling "Add".
For changes the things work fine - but the Add makes problems if I show the Messgebox - if I do nothing (comment out the messagebox)- no problems occure!
The error comes up even if I only call MessageBox.Show("Hello");

Here is the stacktrace of the error:
************** Exception Text **************
System.IndexOutOfRangeException: Index 2 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)
at System.Windows.Forms.DataGridViewRow.GetErrorText(Int32 rowIndex)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Is this a bug - or do I miss something?

Cheers

Manfred

[3544 byte] By [ManniAT] at [2008-2-11]
# 1
Hi Manfred, I'm afraid I can't replicate your error. I was able to add a row on my DGV whose Table had a RowChanging event attached, per your code. No problems... Can you post a simple project that replicates the problem?
durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Hi Durstin,

I reduced the things to the minimum.
This means I use the local SQLEXPRESS instance, and added only 2 lines of code to a new project!
To produce the error I just have to inster a new row. After entering the last field,
I press key down to come to the next (new) record - that's it.

To run the project (without changes) your need a local SQLEXPRESS with the pubs database. Nothing more.
Since I see no way here to attach something - i placed it on one of our servers:
http://ttncf.pp-p.com/TestGridError.zip

--
An other (maybe the same source) problem occured.
In "Rowchanged" I add

try {
theBindingSource.SuspendBinding();
//here is normaly code - but not neede for the error
return;
}
finally{
theBindingSource.ResumeBinding();
}


With this code I get an error - but only if I:
a.) add a record
b.) use the keyboard (down arrow) to navigate to the next (new) row

This error tells "index out of range" in OnPaint of the grid.
I can tell continue - the everything is OK.
When I use the mouse - this error does NOT appear!!

Cheers

Manfred

ManniAT at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Thanks Manfred. I have replicated the problem and am looking into it.
durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Hi --

I am having a very similar issue, but I haven't found a solution.

I have a highly customized DataGrid -- when I use the delete key to delete certain rows, in the DataGridRow.PaintRowHeader, the same exception as above occurs.

Googling around, I found that this happens when CurrencyManager.Current is invalid ( apparently). Unfortunately, I have no idea how to prevent this.

Hoping you find something durstin!
~John

johncov at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
Hi John,

maybe it helps - but I don't need the ChangING event the ChangED is enough for me.
And there I use my own boolean instead of SusbpendBinding - with this no error occures.

Cheers

Manfred

ManniAT at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

We are looking into this, but do you encounter the error only when using MessageBox. If so, try using Debug.WriteLine or some other feeback method than MessageBox.

durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
Hi Durstin,

I encouter the error alos when I call a webservice for an example.
This means "lengthy" operations seem to make problems.
The MessageBox was an example with which the error is 100% redproducalbe.
I found it by checking for errors - and I choose this way since
a.) debugging against a server with hostheader evaluation (and more sites) fails
b.) local debugging would work but the developer werbserver maps to a subdir --
my web is rootbound - so paths like /images/xxx.ashx fail in the developer webserver
BUT - as I told above I can go on with the RowChanged event - and this one does not break if I avoid SuppressBinding and use a private bool instead.

So the problem is not stopping me work.

Thanks for your help

Manfred

ManniAT at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8
This appears to be a bug. The message box is causing the DataGridView to paint at a time when it expects the new row to have already been committed. I have logged a bug against this issue however if you want to track this, you can enter a bug using the link below and we'll link you're bug to the one I've already entered.

Joe

http://lab.msdn.microsoft.com/productfeedback/Default.aspx

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9
Hi Joe,

I did as you suggested.
Here is the link http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackId=FDBK28227

Cheers

Manfred

ManniAT at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10
I had the same (similar problem with DataGrid) where I deleted the last row, then without clicking on any rows I click on the vscrollbar. I get the IndexOutOfRangeException also. then I saw the comment from John Cov where the Current was invalid. So at the end of my delete function I set the CurrencyManager.Position to a valid row. Now the scroll click does not error out.

Kurt

KurtPlace at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...