2

I'm hoping someone can help with an exception I've inherited. Basically I'm writing the rows in a datagrid to a text file. Which works fine apart from when the row is hidden, when the row is hidden the exception "Index was outside the bounds of the array" is thrown at the line highlighted below. Thanks for any help.

DataRow dr;

for (int i = 0; i < bindingManagerBase.Count; i++)

{bindingManagerBase.Position = i;

    dr = ((DataRowView)bindingManagerBase.Current).Row;

    bindingManagerBase.SuspendBinding();
    try
    {

        int rowIndex = dr.Table.Rows.IndexOf(dr);

        if (!rowsDisplayStatus[rowIndex])  //<---------Exception here "Index was outside the bounds of the array" //Picture below
        {
            m_Dgv.Rows[rowIndex].Visible = false;
            continue;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString()); 
    }
    finally
    {
        bindingManagerBase.ResumeBinding();
    }
    writeData(tw, dr);
    iIndex++;
}

alt text

2
  • 1
    You haven't posted enough code. What's rowsDisplayStatus? How and where does it get set? What dimensions does/should it have? ... Commented Aug 5, 2009 at 14:05
  • What is rowIndex? and how is it defined? Commented Aug 5, 2009 at 14:10

6 Answers 6

2

You're getting the row index and then trying to use it with rowsDisplayStatus. You can't use the database row index as an index into your collections.

I would change:

if (!rowsDisplayStatus[rowIndex]) 

to:

if (!rowsDisplayStatus[i]) 
Sign up to request clarification or add additional context in comments.

Comments

1

How is rowsDisplayStatus populated? If it only contains one element and something is expected to be at index 9, you should take a look at the code that populates it.

Comments

0

As the picture shows, rowsDisplayStatus has 1 item in it... you're trying to pull the 10th item (or the item at index 9)... that index is out of range.

Comments

0

How is "rowsDisplayStatus" populated? Maybe there is a problem in that routine.

Comments

0

I'll explain what mark said, above. (Don't know how to comment, so putting this in an answer) He's correct you should change the

if (!rowsDisplayStatus[rowIndex])

Into

if (!rowsDisplayStatus[i])

The reason for this is as follows:

The rowIndex grows even when rows where previously deleted from the Rows object. So there could be only one or two rows in the dr.Table.Rows, but they could have indexes (indice) for example of 8 and 9 (because previously rows 1 to 7 where erased or for other reasons).

So you get the current rowIndex by checking the bindingManager.Current.RowIndex property.

But your rowsDisplayStatus is a simple array (or ArrayList) with the correct amount of rows according to i. So for row index:8 (the first row) you should look at rowsDisplayStatus[0] (which is the value of i), and for row index:9 (the second row) you should look at rowsDisplayStatus[1]... etc.

HTH, Moshe

Comments

0

Basically, whenever I've received this error the index (or value that I have set) was non-existant at the time it was being referenced.

For example if I have two Items in a ListBox and I tryi to reference a third Item, I will receive an Index Out Of Range exception (many times in the past)...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.