开发者

C# LINQ error, can't figure out what the problem is

The program I have displays the contents of lists. Different items within a menu strip allow the user to see the corresponding data within a datagridview when the item is clicked. However for one particular list I have the option to filter the results. When this item is clicked it will bring up a dialog box that allows the user to filter their results. This works and you can then go on to view other lists and then click on the filter item to try a new search. However the problem I am having occurs when you first make a filtered search with or without a filter, then instead of going to another list, you make another filtered search with or without a filter. The error actually occurs when the dialog box appears for the second time and not when you click the button that accepts it.

The error that I recieve reads as follows:

"The following exception occurred in the DataGridView: System.IndexOutOfRangeException: Index 0 does not have a value. at System.Windows.Forms.CurrencyManager.ge_Item(Int32 index) at System.Windows.Forms.DatagridView.DataGridViewDataConnection.GetError(Int32 rowIndex) To replace this default dialog please handle the DataError event."

Within the error there is one part that changes depending on what row has a cell selected. The "Index 0" will change to "Index 3" or whatever based on the row.

The code for this part reads as follows:

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;
            }
        }
    }

If I perform the same thing that creates the error with other items it does not replicate the error. The other items however only change the datasource as I do not need to filter as shown by this:

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

I don't know if I'm just being blind to the real error or not after looking at it for so long, but if anyone can help that would be greatly appreciated.

EDIT:

When the data error event is handled I found out that the error context is the display with the following information given by the stack trace:

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

This repeats 3 times, if you accept the dialog box it carries on without sign of stop.


I've figured out what the problem is. The point at which the error occurs is on the line

parHRes.Clear();

The datagridview doesn't like that the list that is used as a datasource is now null. Adding a line before it to change the datasource to something else fixes this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜