title: How to add row to sorted datagrid view
body: <p>I have a DataGridView bound to a database Table. Visual Studio created the grid and a binding navigator Toolstrip. Navigating, Insert, Delete are working fine...
  BUT:
  If I sort the grid by clicking on some column header, inserting a row is messing up the grid. The row index passed to the RowsAdded event handler is wrong. Its always the index of last row which was fine in unsorted mode before. In sorted mode I am editing the wrong row and see no chance to find out the index of the right one.
  All data bound columns are displayed correctly even though index is always the last row. The problem occurs with not data bound columns. In my case an image column depending on a data bound column. The image is always drawn in the last row. Thus ignoring sorting...</p>
  
  <p>Here is what I am doing in the RowsAdded event handler:</p>
  
  <pre><code>for (int i = 0; i &lt; e.RowCount; ++i)
          {
              DataGridViewRow row = dataGridView_Energietraeger.Rows[i + e.RowIndex];
  
              object idObj = row.Cells[Column_Id.Index].Value;
              if (idObj is System.DBNull || idObj == null)
              {
                  // set default values to all cells in new row...
  
                  // set icon indicating ro/ rw mode - wrong row when sorted!
                  row.ReadOnly = (bool)readonlyObj;
                  SetIconCellImage(row);  
              }
              else
              {
                  object readonlyObj = row.Cells[Column_Readonly.Index].Value;
                  if (!(readonlyObj is System.DBNull) &amp;&amp; readonlyObj != null)
                  {
                      row.ReadOnly = (bool)readonlyObj;
                      SetIconCellImage(row);  //
                  }
              }
          }
  </code></pre>
  
  <p>Any hints?
  May be I did not understood logics of databinding and how to access rows and datasources. but I can't see my mistake yet. Thanks in advance...
  Holger</p>