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(ScrollEven tType type)
at System.Windows.Forms.ScrollBar.WmReflectScroll(Mes sage& m)
at System.Windows.Forms.ScrollBar.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(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.ReflectMessageInterna l(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.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& 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.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at tester.Program.Main() in ...\\tester\\Program.cs:line 19"