Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ArgumentOutOfRangeException未处理(从表单构造函数引发)_C#_.net_Winforms_Datagridview - Fatal编程技术网

C# ArgumentOutOfRangeException未处理(从表单构造函数引发)

C# ArgumentOutOfRangeException未处理(从表单构造函数引发),c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,好的,这里有一个关于我正在处理的事情的稍微详细的概述: 我正在创建一个带有标准化树状视图的表单来选择患者 从该患者的组合框中选择相关作业 在两个DataGridView的顶部 选择两个作业后,每个datagridview将填充相关的 然后可以拖放到另一个服务器的授权 作业的datagridview,以便重新分配它/它们 略不详细的概要: 我正在尝试处理两个DataGridView之间的一系列拖放事件。到目前为止,它们大部分时间都在工作,但是当最后一行从任一gridview中删除,并且表单尝试

好的,这里有一个关于我正在处理的事情的稍微详细的概述:

  • 我正在创建一个带有标准化树状视图的表单来选择患者
  • 从该患者的组合框中选择相关作业 在两个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.
        }
    }
}
我通常不会在这个网站上发布东西,所以如果我在格式或其他方面出错,我很抱歉。如果是这样,请告诉我,我会尽力纠正。如果我需要澄清某些事情或包含更多信息,情况也是如此。 提前感谢任何人可能提供的任何帮助/建议

错误消息:

System.Windows.Forms.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常

其他信息:提供的行索引超出范围

堆栈跟踪:

位于System.Windows.Forms.DataGridViewRowCollection.GetRowState(Int32 rowIndex) 在System.Windows.Forms.DataGridView.OnRowHeaderMouseDown(HitTestInfo hti、Boolean IsShift Down、Boolean isControlDown) 位于System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseevenTargets e) 在System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)中 在System.Windows.Forms.Control.WmMouseDown(Message&m、MouseButtons按钮、Int32单击) 位于System.Windows.Forms.Control.WndProc(Message&m) 位于System.Windows.Forms.DataGridView.WndProc(Message&m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam) 在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中 位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文) 在System.Windows.Forms.Application.Run(Form mainForm)中 在C:\Users\Rhunt\Subversion\WindowsApps\ContractPatients\Job2JobTransfer\Job2JobTransfer\Program.cs中的Job2JobTransfer.Program.Main()处:第19行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处


看起来dataGridView1.DoDragDrop(dataGridView1.Rows[index],DragDropEffects.Move);或者正在调用另一个,但索引无效或行为空或类似情况。 可能是由于某种原因,在表单初始化期间调用了一个EventHandler。您可以从设计器文件中删除EventHandler,并将其移到Form.OnLoad。如果问题仍然出现,则可以在已加载表单的EventHandler中设置加载变量和证明

编辑:您还可以简单地检查dataGridView1和dataGridView2是否在索引处包含行:

int index = e.RowIndex;
if(dataGridView1.Rows.Count() > 0 && index <= dataGridView1.Rows.Count()) {
    dataGridView1.DoDragDrop(dataGridView1.Rows[index], DragDropEffects.Move); 
}
int index=e.RowIndex;

if(dataGridView1.Rows.Count()>0&&index您需要在调试模式下运行程序,以便查看更多详细信息。这是如何使用它的教程:@UçanKartal我确实在调试模式下运行了它。我上面发布的StackTrace是从详细视图中获取的。我是否应该发布其中包含的更多内容?您是否尝试删除
dataGridView2.Refresh();
数据网格视图1.Refresh()
查看问题是否消失?我确实尝试过;是的。唯一改变的是dgvrowadd和remove没有正确显示。嗯。这可能是有意义的。我仍然不理解事件处理程序为什么会触发,但它会解释错误。我不知道为什么会这样。但我曾经遇到过类似的情况,在哪里我有一个带有拖放功能的ListView,它在构造过程中也启动了EventHandler。我从来没有真正弄清楚原因,但将EventHandler分配移动到OnLoad解决了我的问题。好吧,在执行DoDragDrop之前,我继续对行和索引值进行了参数检查。但是,行.Count的值似乎不正确无论什么时候检查它,它都不会更新,程序仍然崩溃。好的,我尝试了你的例子,在我的例子中它是有效的。我创建了两个带有预定义项的组合框(在设计器中)和两个填充的列表
int index = e.RowIndex;
if(dataGridView1.Rows.Count() > 0 && index <= dataGridView1.Rows.Count()) {
    dataGridView1.DoDragDrop(dataGridView1.Rows[index], DragDropEffects.Move); 
}
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();
    }
}