DataGridView滚动条抛出ArgumentOutOfRange异常

时间:2012-04-02 15:25:00

标签: c# winforms datagridview scrollbar

我有一个带有单个DataGridView的表单。 DGV绑定到DataTable并在表单加载大约32,000条记录时填充。

如果用户向下拖动滚动条并在鼠标光标位于滚动条底部的向下箭头上时释放鼠标按钮,我将获得下面列出的例外。

如果鼠标按钮在屏幕底部的任何其他位置释放,在状态栏中向下,在时钟上,则不会抛出任何异常。然后,您可以单击最后一条记录并向上滚动。

此外,如果您将滚动条完全向下拖动,请在“安全”区域中释放鼠标按钮,然后您可以再次滚动并释放向下箭头上的按钮,不会抛出任何异常。

解决方案中的所有DataGridView都使用相同的参数进行设置。我有其他DGV具有相同或更多记录,他们不这样做。

关于Scrollbar Max Value Microsoft说:“只能通过编程方式达到最大值。滚动条的值无法通过运行时的用户交互达到最大值。”

我没有操纵代码中的任何滚动条值。

我无处可寻。我希望有人可以指出我正确的方向来解决这个问题。

System.ArgumentOutOfRangeException was unhandled   Message=Value of
'2612103' is not valid for 'Value'. 'Value' should be between
'minimum' and 'maximum'. Parameter name: Value  
Source=System.Windows.Forms   ParamName=Value   StackTrace:
       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.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr aram, 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.OnMessage(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.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.OnMessage(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(IntPtr
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 System.Windows.Forms.Application.Run(Form mainForm)
       at VWBSMS.Program.Main() in C:\Documents and Settings\courtp\My Documents\Development\C++_DLL_Projects\VWBSMS\VWBSMS\MDIForms\Program.cs:line
21
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()   InnerException:

网格视图

    this.dtGrid.AllowUserToAddRows = false;
    this.dtGrid.AllowUserToDeleteRows = false;
    this.dtGrid.AutoGenerateColumns = false;
    this.dtGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dtGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.idCol,
    this.lineColumn,
    this.lineCol,
    this.eqpCol,
    this.minColumn,
    this.eqptColumn,
    this.rpNameCol,
    this.malCol,
    this.solutionColumn,
    this.ltColumn,
    this.faultColumn,
    this.originatorCol,
    this.dateCol,
    this.timeDownCol,
    this.dateUpCol,
    this.timeUpCol,
    this.T2ID,
    this.T1ID,
    this.threeCodeCol,
    this.respCol});
    this.dtGrid.ContextMenuStrip = this.gvContextMenut;
    this.dtGrid.DataSource = this.dTMPNewBindingSource;
    this.dtGrid.Dock = System.Windows.Forms.DockStyle.Fill;
    this.dtGrid.Location = new System.Drawing.Point(0, 0);
    this.dtGrid.Name = "dtGrid";
    this.dtGrid.ReadOnly = true;
    this.dtGrid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
    this.dtGrid.Size = new System.Drawing.Size(980, 360);
    this.dtGrid.TabIndex = 115;

基类格式化,传递所有网格视图。

        dgvA.AllowUserToAddRows = false;
        dgvA.AllowUserToDeleteRows = false;
        dgvA.AllowUserToOrderColumns = true;
        dgvA.AllowUserToResizeColumns = true;
        dgvA.AllowUserToResizeRows = false;
        dgvA.RightToLeft = System.Windows.Forms.RightToLeft.No;
        dgvA.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

        // Main Grid Settings
        dgvA.BackgroundColor = SystemColors.Control;
        dgvA.BorderStyle = BorderStyle.None;
        dgvA.GridColor = SystemColors.ActiveBorder;
  // ************* Column Headers **********************
            // General Settings
            dgvA.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            dgvA.ColumnHeadersHeight = 20;  // Based on a Tahoma font of 9
            dgvA.ColumnHeadersVisible = true;
            dgvA.Cursor = Cursors.Default;
            dgvA.EnableHeadersVisualStyles = true;


            // Column Headers Default Cell Style
            dgvA.ColumnHeadersDefaultCellStyle.Alignment =             DataGridViewContentAlignment.MiddleCenter;
            dgvA.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
            dgvA.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.WindowText;
            dgvA.ColumnHeadersDefaultCellStyle.SelectionBackColor = SystemColors.MenuHighlight;
            dgvA.ColumnHeadersDefaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
            // ********************** ROWS  ************************
            // Alternating Rows Default Cell Style
            dgvA.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.Info;
            dgvA.AlternatingRowsDefaultCellStyle.ForeColor = SystemColors.WindowText;


            // Row Headers 
            dgvA.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            dgvA.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvA.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control;
            dgvA.RowHeadersDefaultCellStyle.ForeColor = SystemColors.WindowText;
            dgvA.RowHeadersDefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
            dgvA.RowHeadersDefaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
            dgvA.RowHeadersVisible = false;

            // ************* Default Cell Style  ******************
            dgvA.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvA.DefaultCellStyle.BackColor = SystemColors.Window;
            dgvA.DefaultCellStyle.ForeColor = SystemColors.WindowText;
            dgvA.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
            dgvA.DefaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
            dgvA.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

在构造函数

中设置了哪个
    base.gridThemeSet(dtGrid);

    DataGridViewCellStyle myStyle = new DataGridViewCellStyle();

    myStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;


    malCol.DefaultCellStyle = myStyle;

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

dataGridView.PerformLayout();通常可以解决这个问题,但如果它不起作用,只需确保dataGrid中列的插入和删除在活动(启用)时完成,并且它将自动刷新。

答案 2 :(得分:1)

我通过使用表单设计器cs文件中的代码重新创建datagridview控件来解决这个问题:

private void DgvInit(){
    var dgvs=dataGridView1.Size;
    SuspendLayout();
    dis:try{
        dataGridView1.Dispose();
    } catch{goto dis;}
    Controls.Remove(dataGridView1);
    dataGridView1=new DataGridView();
    ((ISupportInitialize)(dataGridView1)).BeginInit();
    dataGridView1.Anchor = ((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right;
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
    dataGridView1.Location = new Point(12, 28);
    dataGridView1.Name = "dataGridView1";
    dataGridView1.Size=dgvs;
    dataGridView1.TabIndex = 0;
    Controls.Add(dataGridView1);
    ((ISupportInitialize)(dataGridView1)).EndInit();
    ResumeLayout(true);
}

如果DGV可能需要调整大小,则需要存储大小并再次分配。

goto使用datagridview修复了一些错误,看起来它可能会在dispose发生异常时导致无限循环,但它还没有我测试过。

答案 3 :(得分:0)

我在向下滚动时出现了相同的错误消息,修复它时遇到了一些麻烦。

最后问题是我有一个数据库表通过datatable绑定到datagrid,db表有一个列varchar(30),我在这个列的单元格中以编程方式编写了一些字符串。这些字符串的长度最多为50。

滚动时出现错误有点奇怪,而不是当我更改数据表值时。该消息也没有提供有用的信息。

以便其他人收到此错误:检查所有数据是否具有正确的数据类型和长度。