By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

dataGridView remove rows in Scroll event cause execption

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.