title: C# Index was outside the bounds of the array
body: <p>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.</p>
  
  <pre><code>DataRow dr;
  
  for (int i = 0; i &lt; bindingManagerBase.Count; i++)
  
  {bindingManagerBase.Position = i;
  
      dr = ((DataRowView)bindingManagerBase.Current).Row;
  
      bindingManagerBase.SuspendBinding();
      try
      {
  
          int rowIndex = dr.Table.Rows.IndexOf(dr);
  
          if (!rowsDisplayStatus[rowIndex])  //&lt;---------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++;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hYD29.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hYD29.png" alt="alt text"></a>
  </p>