C#LINQ错误,无法弄清问题是什么

时间:2011-09-05 14:40:42

标签: c# linq data-binding datagridview dialog

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

我收到的错误内容如下:

“DataGridView中发生以下异常: System.IndexOutOfRangeException:索引0没有值。 System.Windows.Forms.DatagridView.DataGridViewDataConnection.GetError(Int32 rowIndex)中的System.Windows.Forms.CurrencyManager.ge_Item(Int32索引) 要替换此默认对话框,请处理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次,如果你接受它继续进行的对话框,没有停止的迹象。

1 个答案:

答案 0 :(得分:1)

我已经弄明白了问题所在。发生错误的位置在

行上
parHRes.Clear();

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