【发布时间】:2016-01-21 21:22:39
【问题描述】:
我有一个数据网格视图,左侧有 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:
【问题讨论】:
标签: c# events datagridview indexoutofboundsexception