Renuka
Renuka

Reputation: 25

Unexpected Exception While updating DataGridView : " Index was outside the bounds of the array" even if index is proper

I am trying to update DataGridView. While updating it throws exception randomly.(Not every time).

 foreach (DataGridViewRow row in dataGrdVwReport.Rows)
 {
       try
      {   
          f_RowIndex = row.Index;
          f_ColCount = row.Cells.Count;
          row.Cells[2].Value = p_Count;  //Exception here
      }
      catch (Exception ex)
      {
          m_objLogFile.Error(CLASS_NAME + "->" + METHOD_NAME + "() : inner foreach : " + "f_RowIndex = " + f_RowIndex);
          m_objLogFile.Error(CLASS_NAME + "->" + METHOD_NAME + "() : inner foreach : " + "f_ColCount = " + f_ColCount);
          m_objLogFile.Error(CLASS_NAME + "->" + METHOD_NAME + "() : inner foreach : " + "p_Count= " + p_Count);
          m_objLogFile.Error(CLASS_NAME + "->" + METHOD_NAME + "() : inner foreach : " + ex.Message);
      }

Exception is:

  Index was outside the bounds of the array.

Stacktrace is:

at System.Windows.Forms.PropertyStore.GetObject(Int32 key, Boolean& found) at System.Windows.Forms.DataGridViewCellStyle.set_SelectionBackColor(Color value) at System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, Int32 rowIndex, Boolean includeColors) at System.Windows.Forms.DataGridViewCell.GetPreferredHeight(Int32 rowIndex, Int32 width) at System.Windows.Forms.DataGridViewRow.GetPreferredHeight(Int32 rowIndex, DataGridViewAutoSizeRowMode autoSizeRowMode, Boolean fixedWidth) at System.Windows.Forms.DataGridView.AutoResizeRowInternal(Int32 rowIndex, DataGridViewAutoSizeRowMode autoSizeRowMode, Boolean fixedWidth, Boolean internalAutosizing) at System.Windows.Forms.DataGridView.OnCellCommonChange(Int32 columnIndex, Int32 rowIndex) at System.Windows.Forms.DataGridView.OnCellValueChanged(DataGridViewCellEventArgs e) at System.Windows.Forms.DataGridView.OnCellValueChangedInternal(DataGridViewCellEventArgs e) at System.Windows.Forms.DataGridViewCell.SetValue(Int32 rowIndex, Object value) at System.Windows.Forms.DataGridViewCell.set_Value(Object value)

Fisrt time exception occurs, Count was: RowIndex = 0 : RowCount = 1; ColumnCount = 9 ; P_Count 15

I tryed it again: RowIndex = 0 : RowCount = 1; ColumnCount = 9 ; P_Count 93

Again it was: RowIndex = 0 : RowCount = 1; ColumnCount = 9 ; P_Count 7

If I delete complete row & reinsert with updated value.. In that case there is no exception.. But I want to edit that particular cell.. & I think that deleting & reinserting row is not a proper way.. It's time consuming..

Please suggest solution.

Upvotes: 1

Views: 987

Answers (1)

GuidoG
GuidoG

Reputation: 12059

Have you tried something like this to find out where your error comes from ? After you have found and fixed the error you can remove the try catch

foreach (DataGridViewRow row in dataGrdVwReport.Rows)
{
   try
   {
       row.Cells[2].Value = p_Count;  //Exception here
   }
   catch (exception ex)
   {
       // write code here to log the rowcount, columncount, p_count, and other stuff here
       throw;
   }
}

Upvotes: 0

Related Questions