DataTable.Rows.Remove Exception

T

Trevor

Hi

I have written a database winform app, and it performs well in testing.

When I installed the app onto my client's site I get an exception on
removing a new row that has been added to the table rows collection,
i.e.cancelling a new row.
The table is loosely bound to a data grid, the grid cells being populated by
code, and the error blanks out the Data Grid in white with a red cross. The
exception raised is "No value at Index 0" This is raised by the
system.windows.forms.Currencymanager.get_item(int32 index) inresponse to the
System.Windows.Forms.DataGridRow.PaintHeader event.

I have tracked the exception to be in response to the remove method of the
table rows.

The code I use to add the row is

drNewRow = Table.NewRow();
Table.Rows.Add(drNewRow);

and the code to remove the row is

Table.Rows.Remove(drNewRow); <- exception raised here. Commenting out this
row removes exception.

drNewRow = null;

I do not get this in the Windows XP Pro test environment, but a new Windows
XP Pro installed test machine has the same problem. I am using the same
user with both machines. The client tells me that the exception goes away
after a number of rows have been added to the SQL Server database.

Does any one know the basis of the problem, and how to fix it?

Thanks
Trevor
 
G

Guest

Trevor,
You might try to delete the row using:

drNewRow.Delete

rather than removing the row from the table and setting it to nothing.

Dave
 
T

Trevor

Dave

Tried deleting on the row, but the exception persists.

I lied about the coupling, it was some time since I wrote this code, the
table is assigned to the DataSource property.

Does this change your suggested solution?

Trevor
 
K

Kevin Yu [MSFT]

Hi Trevor,

Could you let me know what type of exception is thrown? Also, if you can
build a small repro and send to my email box, I can deliver my assistance
more quickly with that repro.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

Trevor

Kevin

The exception i get is:

************** Exception Text **************
System.IndexOutOfRangeException: No value at index 0.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridRow.PaintHeader(Graphics g, Rectangle
visualBounds, Boolean alignToRight, Boolean rowIsDirty)
at
System.Windows.Forms.DataGridRelationshipRow.PaintHeaderInside(Graphics g,
Rectangle bounds, Brush backBr, Boolean alignToRight, Boolean isDirty)
at System.Windows.Forms.DataGridRelationshipRow.PaintHeader(Graphics g,
Rectangle bounds, Boolean alignToRight, Boolean isDirty)
at System.Windows.Forms.DataGrid.PaintRows(Graphics g, Rectangle&
boundingRect)
at System.Windows.Forms.DataGrid.PaintGrid(Graphics g, Rectangle
gridBounds)
at System.Windows.Forms.DataGrid.OnPaint(PaintEventArgs pe)
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.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************

I will look at creating a minmum project that exhibits the problem.

Thanks
Trev.
 
K

Kevin Yu [MSFT]

Thanks, Trev.

Remove 'online' from the nospam alias is my email.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
C

Cor Ligthert

Trevor,

What seems strange to me how this can work.
Table.Rows.Remove(drNewRow); <- exception raised here. Commenting out
this row removes exception.

drNewRow = null;
It is strange that it gives different behaviour, although can it maybe have
to do with the GC time.

What you tell here is to remove an object, (A remove is really a remove from
an object).

You use the removed reference to set that to null. Strange that this can
work, I can only explain this for myself when I think how the GC works.

For a delete from an *in advance existing* row, would this code be possible
before an acceptchanges is done, because than the rowstate is only set.

Just my thought,

Cor
 
G

Guest

Trevor,
Is the table a member of a strongly typed dataset? If so, have you tried
making the Dataset the DataSource and the Table the DataMember?

Does this error occur even if their are other rows left after the delete?

I believe in some binding scenarios, where the source is not strongly typed,
the datagrid looks to the first row of data for info on Column Names to paint
the header row. Sounds like it is not finding a row 0 because it has been
deleted.

Regards,
Dave
 
T

Trevor

Hi All

After some further investigation the problm only occurs on Intel Processors.
Athlons do not demonstrate this problem.

So the problem is either in the Thread handling, or Microcode in the
processor.

Either way it is outside my control.

Thanks
Trevor
 
D

Dick Walker

Trevor said:
Hi All

After some further investigation the problm only occurs on Intel Processors.
Athlons do not demonstrate this problem.

So the problem is either in the Thread handling, or Microcode in the
processor.

Either way it is outside my control.

Thanks
Trevor

Happens on my AMD Athlon 64 too...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top