title: CellValidating error on cell focus
body: <p>I got 2 Datagridview in my form and I'm using  cell validation and cell validating event handler.. in my datagridview. If i leave the cursor in the cell itself and try to open a new file through menu click..i'm getting a error message : "Argument out of exception was unhandled by usercode, index was out of range."</p>
  
  <p>I know, when the cursor is in the cell, it got focus and it's in the process of cell validating that's why i'm getting this error.  </p>
  
  <p>This line throws the error with the message : "Argument out of exception was unhandled by usercode, index was out of range."
  <code>datagridview.Rows[e.RowIndex].ErrorText = "";</code></p>
  
  <p>How can i avoid this error?? Or How can i remove focus during menu click to open a new file?? Thank you.</p>
  
  <pre><code>private void datagridview_CellValidating(object sender, CellValidatingEventArgs e)
              {
                  if (e.ColumnInfo.Name == "Item1" || e.ColumnInfo.Name == "Item2")
                  {
                      datagridview.Rows[e.RowIndex].ErrorText = "";
                      int newInteger;
                      if (datagridview.Rows[e.RowIndex].IsModified)
                          return;
                      if (!int.TryParse(e.Value.ToString(),
                           out newInteger) || newInteger &lt; -50000 || newInteger &gt; 50000)
                      {
                          e.Cancel = true;
                          datagridview.Rows[e.RowIndex].ErrorText = "The value must be a non-negative integer";
                      }
                  }
  
              }
  
   private void datagridview_CellValidated(object sender, CellValidatedEventArgs e)
              {
  
                  if (e.ColumnIndex != 3)
                      return;
                  int nextRowIndex = e.RowIndex + 1;
                  int lastRowIndex = datagridview.Rows.Count - 1;
                  if (nextRowIndex &lt;= lastRowIndex)
                  {
                      var value = datagridview.Rows[e.RowIndex].Cells[3].Value.ToString();
                      datagridview.Rows[nextRowIndex].Cells[2].Value = value;
                  }
              }
  </code></pre>
  
  <p>MenuItem Clik to open new files to the Datagridview:</p>
  
  <pre><code>private void m_test1_Click_1(object sender, EventArgs e)
      {
          myconfig = Myconfig.DeserializeFromXmlFile(@"test2.xml");
          Display(rseConfig);
          m_ConfigPages.Visible = true;
  
      }
  
  
  
  private void m_test2_Click(object sender, EventArgs e)
    {
          myconfig = Myconfig.DeserializeFromXmlFile(@"test2.xml");
          Display(myconfig );
          m_ConfigPages.Visible = true;
  
      }
  </code></pre>
  
  <p>I'm using DataError Event Hnadler, but it's not working:</p>
  
  <pre><code>private void datagridview_DataError(object sender, DataGridViewDataErrorEventArgs e)
          {
              // Don't throw an exception when we're done.
              e.ThrowException = false;
  
              // Display an error message.
              string txt = "Error with " +
                  datagridview.Columns[e.ColumnIndex].HeaderText +
                  "\n\n" + e.Exception.Message;
              MessageBox.Show(txt, "Error",
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
  
              // If this is true, then the user is trapped in this cell.
              e.Cancel = true;
          }
  </code></pre>
  
  <p>Even, i have with row and col index : <code>datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value</code> still not working.</p>