ArgumentOutOfRangeException未处理(从Form构造函数抛出)
本文关键字:构造函数 Form 未处理 ArgumentOutOfRangeException | 更新日期: 2023-09-27 18:17:06
好,下面是我要处理的稍微详细的概述:
- 我正在创建一个带有标准化树视图的表单来选择患者。
- 从组合框中选择该患者的相关作业在两个datagridview的顶部。
- 一旦选择了两个作业,每个datagridview将填充相关的授权,然后可以将其拖放到另一个作业的datagridview,以便重新分配它/它们。
略不详细的简介:
我试图处理两个datagridview之间的拖放事件序列。到目前为止,它们大部分时间都可以工作,但是当最后一行从gridview中删除时,并且表单尝试刷新时,Program.cs文件在尝试创建新表单对象时抛出异常。
Program.cs文件中的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Job2JobTransfer
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // this is where the error gets thrown.
}
}
}
看起来很简单,对吧?这让我怀疑是否在构造函数中隐藏了一些我没有考虑到的东西,或者是否我太迟钝而没有意识到自己的错误。
下面是我按照dgv1_mousedown、dgv2_dragEnter和dgv2_dragDrop的顺序为拖放事件编写的代码。相反是相同的代码,只是重命名。
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
int index = e.RowIndex;
dataGridView1.DoDragDrop(dataGridView1.Rows[index], DragDropEffects.Move);
}
private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
int index = e.RowIndex;
dataGridView2.DoDragDrop(dataGridView2.Rows[index], DragDropEffects.Move);
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
DataGridView otherGridView = (DataGridView)sender;
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
if (!dataGridView1.Rows.Contains((DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow))))
{
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
DataGridView otherGridView = (DataGridView)sender;
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
if (!dataGridView2.Rows.Contains((DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow))))
e.Effect = DragDropEffects.Move;
}
else
e.Effect = DragDropEffects.None;
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow tempRow = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
WorkAuthorization tempAuth = new WorkAuthorization();
tempAuth = (WorkAuthorization)tempRow.DataBoundItem;
dataGridView2.DataSource = null;
workAuthList2.Add(tempAuth);
dataGridView2.DataSource = workAuthList2;
try
{
dataGridView1.DataSource = null;
workAuthList1.Remove(tempAuth);
dataGridView1.DataSource = workAuthList1;
}
catch
{
int index = 0; //For the sake of a break point while debugging
}
dataGridView2.Refresh();
dataGridView1.Refresh();
}
我通常不会在这个网站上发布东西,所以如果我在格式或其他方面犯了错误,我道歉。如果有,请告诉我,我会尽力改正的。如果我需要澄清一些事情或包含更多信息,也可以这样做。提前感谢任何人可能提供的任何帮助/建议。
错误提示:
类型为"System"的未处理异常。在System.Windows.Forms.dll中发生了ArgumentOutOfRangeException
附加信息:提供的行索引超出范围。
异常堆栈:
在System.Windows.Forms.DataGridViewRowCollection。GetRowState (Int32 rowIndex)在System.Windows.Forms.DataGridView。OnRowHeaderMouseDown(HitTestInfo hti, Boolean isshifdown, Boolean isControlDown)在System.Windows.Forms.DataGridView。OnCellMouseDown (DataGridViewCellMouseEventArgs e)在System.Windows.Forms.DataGridView。OnMouseDown (MouseEventArgs e)在System.Windows.Forms.Control.WmMouseDown (Message&m,鼠标按钮,点击32下)在System.Windows.Forms.Control.WndProc (Message&米)在System.Windows.Forms.DataGridView.WndProc (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message&米)在System.Windows.Forms.NativeWindow。DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG&味精)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager。fpushmessagelloop (IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)在System.Windows.Forms.Application.ThreadContext。RunMessageLoopInner(Int32 reason, ApplicationContext context)在System.Windows.Forms.Application.ThreadContext。runmessagelloop (Int32 reason, ApplicationContext context)在System.Windows.Forms.Application。运行(mainForm形式)在c:' users ' rhuntsman ' subversion' WindowsApps'ContractPatients'Job2JobTransfer'Job2JobTransfer'Program.cs:第19行在System.AppDomain。_nExecuteAssembly(运行时汇编程序集,字符串[]args)在System.AppDomain。ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()在System.Threading.ThreadHelper。ThreadStart_Context(对象状态)在System.Threading.ExecutionContext。RunInternal(ExecutionContext, ExecutionContext, ContextCallback, callback, Object state, Boolean, preserveSyncCtx)在System.Threading.ExecutionContext。运行(ExecutionContext ExecutionContext, ContextCallback, callback, Object state, Boolean, preserveSyncCtx)在System.Threading.ExecutionContext。运行(ExecutionContext, ExecutionContext, ContextCallback, callback, Object state)在System.Threading.ThreadHelper.ThreadStart ()
似乎dataGridView1. dodragdrop (dataGridView1. dodragdrop)。行(指数),DragDropEffects.Move);或者另一个被调用,但索引无效或者行是空的,诸如此类。可能是由于某种原因,在窗体初始化期间调用了其中一个事件处理程序。可以从设计器文件中删除事件处理程序,并将其移动到窗体中。OnLoad。如果问题仍然存在,您可以设置一个加载变量,并在事件处理程序中证明表单是否已加载。
编辑:你也可以简单地检查如果dataGridView1和dataGridView2包含行在索引:
int index = e.RowIndex;
if(dataGridView1.Rows.Count() > 0 && index <= dataGridView1.Rows.Count()) {
dataGridView1.DoDragDrop(dataGridView1.Rows[index], DragDropEffects.Move);
}
编辑2:这段代码对我来说没有问题:
public class WorkAuthorization
{
public string Name { get; set; }
public string Description { get; set; }
}
public partial class Form1 : Form
{
private List<WorkAuthorization> workAuthList1;
private List<WorkAuthorization> workAuthList2;
public Form1()
{
InitializeComponent();
workAuthList1 = new List<WorkAuthorization>();
workAuthList1.Add(new WorkAuthorization { Name = "Authorization1", Description = "Description1" });
workAuthList1.Add(new WorkAuthorization { Name = "Authorization2", Description = "Description2" });
workAuthList1.Add(new WorkAuthorization { Name = "Authorization3", Description = "Description3" });
workAuthList2 = new List<WorkAuthorization>();
workAuthList2.Add(new WorkAuthorization { Name = "Authorization4", Description = "Description4" });
workAuthList2.Add(new WorkAuthorization { Name = "Authorization5", Description = "Description5" });
workAuthList2.Add(new WorkAuthorization { Name = "Authorization6", Description = "Description6" });
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
LoadAuthorizations1();
}
private void LoadAuthorizations1()
{
dataGridView1.DataSource = workAuthList1;
}
private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
{
LoadAuthorizations2();
}
private void LoadAuthorizations2()
{
dataGridView2.DataSource = workAuthList2;
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
int index = e.RowIndex;
dataGridView1.DoDragDrop(dataGridView1.Rows[index], DragDropEffects.Move);
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
DataGridView otherGridView = (DataGridView)sender;
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
if (!dataGridView1.Rows.Contains((DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow))))
{
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
DataGridView otherGridView = (DataGridView)sender;
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
if (!dataGridView2.Rows.Contains((DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow))))
{
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow tempRow = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
WorkAuthorization tempAuth = new WorkAuthorization();
tempAuth = (WorkAuthorization)tempRow.DataBoundItem;
dataGridView2.DataSource = null;
workAuthList2.Add(tempAuth);
dataGridView2.DataSource = workAuthList2;
try
{
dataGridView1.DataSource = null;
workAuthList1.Remove(tempAuth);
dataGridView1.DataSource = workAuthList1;
}
catch
{
int index = 0; //For the sake of a break point while debugging
}
dataGridView2.Refresh();
dataGridView1.Refresh();
}
}