dataGridView remove rows in Scroll event cause execption

M

Michael Meckelein

I get "Value of '3720' is not valid for 'Value'. 'Value' should be
between 'minimum' and 'maximum' exception if I remove rows in a
dataGridView while scrolling from the top to the end of the grid in
one fell swoop. You can reproduce this using the code listed under
[1]. .NET 2.0 is used.

The only description of the exception found at Microsoft [2] was not
helpful.

As the exception occurs in
System.Windows.Forms.ScrollBar.set_Value(Int32 value) (see [3] for the
stack trace) I guess I have to reset a pointer or cancel the scrolling
event if it tries to scroll to rows which are not longer available.
But how to do that?

Note that the source code snippet below is not the actual program
code. But it perfectly illustrates the issue. Important is that you
scroll in one single move from the top to the end. Wondering if you
scroll row by row, the exception does not occur.

Thanks in advance.

Michael

[1] Program source code snippet


int testCount = 200;
public Form1()
{
InitializeComponent();
this.dataGridView1.Rows.Add(testCount);
}
private void dataGridView1_Scroll(object sender,
ScrollEventArgs e)
{
if (e.NewValue > 150)
{
for (int i = testCount; i > 150; i--)
{
this.dataGridView1.Rows.RemoveAt(i - 1);
}
e.NewValue = 100;
this.dataGridView1.FirstDisplayedScrollingRowIndex =
100;
}
}

[2] "System.ArgumentException: '0' is not a valid value for 'value'"
error message when you populate a DataGrid control in a Windows Forms
application
http://support.microsoft.com/kb/838087

[3] Exception and stack trace

Message = "Value of '3720' is not valid for 'Value'. 'Value' should be
between 'minimum' and 'maximum'.\r\nParameter name: Value"
Source "System.Windows.Forms" string


" at System.Windows.Forms.ScrollBar.set_Value(Int32 value)
at System.Windows.Forms.ScrollBar.DoScroll(ScrollEventType type)
at System.Windows.Forms.ScrollBar.WmReflectScroll(Message& m)
at System.Windows.Forms.ScrollBar.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef
hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr
wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,
Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseDown(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollBar.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at tester.Program.Main() in ...\\tester\\Program.cs:line 19"
 
M

Michael Meckelein

I must admit that this seems a curious place to be deleting rows...
perhaps virtual mode would be a better solution?

http://msdn2.microsoft.com/en-us/library/2b177d6d.aspx

Marc

Thanks for the hint Marc. "Virtual mode is designed for use with very
large stores of data" which is exactly what I have to handle. However,
I still getting the exception even deleting code was moved to

private void dataGridView_CellValueNeeded(object sender,
DataGridViewCellValueEventArgs e)

The issue I think is that I have a total account of rows which is
changing during runtime and which I have to handle. Adding rows is no
problem. But deleting if you scroll as described resulting in an
exception.

Does anyone could tell me the best practices to handle changing amount
of total rows in a dataGridView? The gird is not bound to an object.
Adding/deleting rows have to be coded.

Michael
 

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