数据网格视图索引越界错误在事件中未设置索引

本文关键字:索引 事件 设置 越界 数据网 网格 视图 数据 错误 | 更新日期: 2023-09-27 18:31:26

我有一个数据网格视图,左侧有 3 个冻结列,然后我希望的第 4 列始终显示在右侧。其余列显示在第 3 列和第 4 列之间,并在其标题中编号(使用 1、2、3、4 等)。我允许用户对这些列重新排序,但我希望列标题保持数字顺序。这个想法是用户应该将其视为对列中的数据进行重新排序,而不是列本身。第 4 列标记为"新建",如果用户尝试将数据放入其中,则会创建一个新列并将数据添加到其中,这就是我希望它始终位于右侧的部分原因。

为此,我使用以下 ColumnDisplayIndexChanged 事件:

private void dgvTreeLevels_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e) {
    switch(e.Column.Index) {
       case 0:
       case 1:
       case 2: //prevents columns 0, 1, 2 from being reordered, (freezing just prevents them from being switched with 3+)
          SynchronizationContext.Current.Post(delegate(object o) {
             if (e.Column.DisplayIndex != e.Column.Index) e.Column.DisplayIndex = e.Column.Index;
          }, null);
          break;
       case 3: //Displays always on the right
          SynchronizationContext.Current.Post(delegate(object o) {
             if (e.Column.DisplayIndex != dgvTreeLevels.Columns.Count - 1)
                e.Column.DisplayIndex = dgvTreeLevels.Columns.Count - 1;
          }, null);
          break;
       default: //Numbered columns
          e.Column.HeaderText = (e.Column.DisplayIndex - 2).ToString();
          break;
    }
 }

大多数时候它工作正常。但偶尔这条线

              e.Column.HeaderText = (e.Column.DisplayIndex - 2).ToString();

正在抛出一个 ArgumentOutOfRangeException,指出索引超出范围。由于它没有设置任何索引,因此这尤其令人困惑。我无法可靠地创建错误。它只是在我测试功能时偶尔弹出。我认为这可能与尝试将编号列移动到最右侧有关,但是当我做其他事情时,错误稍后会弹出。

签入调试器显示行中的所有元素都已定义并具有正确的值。我不知道为什么会出现错误。堆栈跟踪是

System.ArgumentOutOfRangeException was unhandled
  Message="Index was out of range. Must be non-negative and less than the size of the collection.'r'nParameter name: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
       at System.Windows.Forms.DataGridView.GetColumnDisplayRectanglePrivate(Int32 columnIndex, Boolean cutOverflow)
       at System.Windows.Forms.DataGridView.GetCellDisplayRectangle(Int32 columnIndex, Int32 rowIndex, Boolean cutOverflow)
       at System.Windows.Forms.DataGridView.GetCellAdjustedDisplayRectangle(Int32 columnIndex, Int32 rowIndex, Boolean cutOverflow)
       at System.Windows.Forms.DataGridView.InvalidateCellPrivate(Int32 columnIndex, Int32 rowIndex)
       at System.Windows.Forms.DataGridView.OnColumnHeaderGlobalAutoSize(Int32 columnIndex)
       at System.Windows.Forms.DataGridView.OnCellValueChanged(DataGridViewCellEventArgs e)
       at System.Windows.Forms.DataGridViewColumnHeaderCell.SetValue(Int32 rowIndex, Object value)
       at System.Windows.Forms.DataGridViewColumn.set_HeaderText(String value)
       at Customizable_Reports.frmReport.dgvTreeLevels_ColumnDisplayIndexChanged(Object sender, DataGridViewColumnEventArgs e)
       at System.Windows.Forms.DataGridView.FlushDisplayIndexChanged(Boolean raiseEvent)
       at System.Windows.Forms.DataGridView.CorrectColumnDisplayIndexesAfterDeletion(DataGridViewColumn dataGridViewColumn)
       at System.Windows.Forms.DataGridView.OnRemovedColumn_PreNotification(DataGridViewColumn dataGridViewColumn)
       at System.Windows.Forms.DataGridViewColumnCollection.RemoveAtInternal(Int32 index, Boolean force)
       at System.Windows.Forms.DataGridViewColumnCollection.RemoveAt(Int32 index)
       at Customizable_Reports.frmReport.RemoveEmptyColumns()
       at Customizable_Reports.frmReport.dgvTreeLevels_CellClick(Object sender, DataGridViewCellEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       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.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 Customizable_Reports.Program.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

数据网格视图索引越界错误在事件中未设置索引

进一步的测试表明,当将编号较高的列移动到编号较低的列前面,然后删除时,会出现错误。然后,当由于删除而在 ColumnDisplayIndexChanged 事件中更新了编号较低的列的标题文本时,将发生此错误。

您可以通过创建新的 Winform 应用程序,在具有两列的窗体上放置一个数据网格视图并允许用户对它们重新排序来测试这一点。添加一个按钮和以下两个事件:

  private void button1_Click(object sender, EventArgs e) {
     dataGridView1.Columns.RemoveAt(1);
  }
  private void dataGridView1_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e) {
     e.Column.HeaderText = e.Column.DisplayIndex.ToString();
  }

运行应用程序,将第 2 列拖到第 1 列前面,然后按 按钮。请注意,必须为列提供唯一的标头才能发生此错误。改为设置e.Column.HeaderText = "test";不会产生错误。

对我来说,这似乎是WinForms中的一个错误,而不是在这个编程中。但我对正在发生的事情还不够了解,无法确定。

将命令传递到当前同步上下文似乎可以解决此问题:

SynchronizationContext.Current.Post(delegate(object o) {
     e.Column.HeaderText = e.Column.DisplayIndex.ToString();
}, null);