469,917 Members | 1,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

dataGridView remove rows in Scroll event cause execption

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"

May 21 '07 #1
2 4026
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
May 21 '07 #2
On May 21, 1:20 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
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

May 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by lord.zoltar | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
reply views Thread by =?Utf-8?B?aW1yYW4uYQ==?= | last post: by
reply views Thread by =?Utf-8?B?S3VtYXIuQS5QLlA=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.