c# LINQ错误,无法找出问题所在

本文关键字:出问题 LINQ 错误 | 更新日期: 2023-09-27 18:07:29

程序显示列表的内容。菜单条中的不同项允许用户在单击项时看到数据视图中相应的数据。但是,对于一个特定的列表,我可以选择过滤结果。当单击此项时,将弹出一个对话框,允许用户过滤结果。这是有效的,然后您可以继续查看其他列表,然后单击过滤项来尝试新的搜索。然而,我遇到的问题是,当您首先使用过滤器或不使用过滤器进行过滤搜索时,然后不是去另一个列表,而是使用过滤器或不使用过滤器进行另一个过滤搜索。该错误实际上是在对话框第二次出现时发生的,而不是在您单击接受它的按钮时发生的。

我收到的错误如下:

" DataGridView中出现以下异常:系统。IndexOutOfRangeException:索引0没有值。在System.Windows.Forms.CurrencyManager。ge_Item(Int32索引)在System.Windows.Forms.DatagridView.DataGridViewDataConnection。GetError (Int32 rowIndex)要替换此默认对话框,请处理DataError事件。"

错误中有一个部分会根据选择了哪个行中的单元格而改变。"索引0"将根据行更改为"索引3"或其他值。

这部分的代码如下:

private void mnusViewParagraphHistory_Click(object sender, EventArgs e)
    {
        ViewHistoryFilter histFilter = new ViewHistoryFilter();
        int idFilt;
        string fundIDFilt = "";
        string changedBFilt = "";
        parHRes.Clear();
        if (parH.Count != 0)
        {
            if (histFilter.ShowDialog(this) == DialogResult.OK)
            {
                var parahistQuery = from his in parH
                                    select his;
                if (histFilter.txtID.Text.Trim() != "")
                {
                    idFilt = Convert.ToInt32(histFilter.txtID.Text.Trim());
                    parahistQuery = parahistQuery.Where(h => h.ID == idFilt);
                }
                if (histFilter.txtFundID.Text.Trim() != "")
                {
                    fundIDFilt = histFilter.txtFundID.Text.Trim();
                    parahistQuery = parahistQuery.Where(h => h.FundID.Contains(fundIDFilt.Trim()));
                }
                if (histFilter.txtChangedBy.Text.Trim() != "")
                {
                    changedBFilt = histFilter.txtChangedBy.Text.Trim();
                    parahistQuery = parahistQuery.Where(h => h.ChangedBy.Contains(changedBFilt.Trim()));
                }
                parHRes.AddRange(parahistQuery);
            }
            if (dataGridView1.DataSource != parHRes)
            {
                dataGridView1.DataSource = parHRes;
            }
        }
    }

如果我对其他项目执行创建错误的相同操作,它不会复制错误。然而,其他项只更改数据源,因为我不需要进行筛选,如下所示:

private void mnusViewParagraph_Click(object sender, EventArgs e)
    {
        if (dataGridView1.DataSource != parG)
        {
            dataGridView1.DataSource = parG;
        }
    }

看了这么长时间,我不知道我是否只是对真正的错误视而不见,但如果有人能帮助我,我将不胜感激。

编辑:

当数据错误事件被处理时,我发现错误上下文是由堆栈跟踪给出的以下信息的显示:

dataGridView1_DataError at offset 433 in file:line:column <filename unknown>:0:0
OnDataError at offset 370 in file:line:column <filename unknown>:0:0
OnDataErrorInternal at offset 47 in file:line:column <filename unknown>:0:0
GetError at offset 156 in file:line:column <filename unknown>:0:0
GetErrorText at offset 149 in file:line:column <filename unknown>:0:0
Paint at offset 179 in file:line:column <filename unknown>:0:0
PaintRows at offset 1551 in file:line:column <filename unknown>:0:0
PaintGrid at offset 675 in file:line:column <filename unknown>:0:0
OnPaint at offset 785 in file:line:column <filename unknown>:0:0
PaintWithErrorHandling at offset 161 in file:line:column <filename unknown>:0:0
WmPaint at offset 831 in file:line:column <filename unknown>:0:0
WndProc at offset 689 in file:line:column <filename unknown>:0:0
WndProc at offset 275 in file:line:column <filename unknown>:0:0
OnMessage at offset 19 in file:line:column <filename unknown>:0:0
WndProc at offset 49 in file:line:column <filename unknown>:0:0
DebuggableCallback at offset 100 in file:line:column <filename unknown>:0:0
DispatchMessageW at offset 0 in file:line:column <filename unknown>:0:0
System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop at offset 647 in file:line:column <filename unknown>:0:0
RunMessageLoopInner at offset 364 in file:line:column <filename unknown>:0:0
RunMessageLoop at offset 97 in file:line:column <filename unknown>:0:0
RunDialog at offset 51 in file:line:column <filename unknown>:0:0
ShowDialog at offset 911 in file:line:column <filename unknown>:0:0
mnusViewParagraphHistory_Click at offset 273 in file:line:column <filename unknown>:0:0
RaiseEvent at offset 115 in file:line:column <filename unknown>:0:0
OnClick at offset 70 in file:line:column <filename unknown>:0:0
HandleClick at offset 201 in file:line:column <filename unknown>:0:0
HandleMouseUp at offset 520 in file:line:column <filename unknown>:0:0
FireEventInteractive at offset 137 in file:line:column <filename unknown>:0:0
FireEvent at offset 280 in file:line:column <filename unknown>:0:0
OnMouseUp at offset 178 in file:line:column <filename unknown>:0:0
OnMouseUp at offset 38 in file:line:column <filename unknown>:0:0
WmMouseUp at offset 721 in file:line:column <filename unknown>:0:0
WndProc at offset 2362 in file:line:column <filename unknown>:0:0
WndProc at offset 42 in file:line:column <filename unknown>:0:0
WndProc at offset 74 in file:line:column <filename unknown>:0:0
WndProc at offset 54 in file:line:column <filename unknown>:0:0
OnMessage at offset 19 in file:line:column <filename unknown>:0:0
WndProc at offset 49 in file:line:column <filename unknown>:0:0
DebuggableCallback at offset 100 in file:line:column <filename unknown>:0:0
DispatchMessageW at offset 0 in file:line:column <filename unknown>:0:0
System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop at offset 647 in file:line:column <filename unknown>:0:0
RunMessageLoopInner at offset 364 in file:line:column <filename unknown>:0:0
RunMessageLoop at offset 97 in file:line:column <filename unknown>:0:0
Run at offset 49 in file:line:column <filename unknown>:0:0
Main at offset 77 in file:line:column <filename unknown>:0:0
_nExecuteAssembly at offset 0 in file:line:column <filename unknown>:0:0
ExecuteAssembly at offset 109 in file:line:column <filename unknown>:0:0
RunUsersAssembly at offset 42 in file:line:column <filename unknown>:0:0
ThreadStart_Context at offset 99 in file:line:column <filename unknown>:0:0
Run at offset 176 in file:line:column <filename unknown>:0:0
Run at offset 44 in file:line:column <filename unknown>:0:0
ThreadStart at offset 68 in file:line:column <filename unknown>:0:0

这个重复3次,如果你接受这个对话框,它会继续,没有停止的迹象。

c# LINQ错误,无法找出问题所在

我知道问题出在哪里了。发生错误的点在

行上
parHRes.Clear();

datagridview不喜欢用作数据源的列表现在为空。在它之前添加一行以将数据源更改为其他内容可以解决此问题。