Negative rowIndex for new DataGridViewRow

Brady Kelly <[email protected]> Sun, 27 Jan 2008 15:55:33 +0200
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
I create and add a new row to an unbound DataGridView.  Then, if I try and
access an item in the Cells collection of the row, as in my commented out
code, I get an index out of range exception, rowIndex.  I'm not specifying a
row index, so I assume it is inferring it from the row object, whose
rowIndex property is -1.  However, when I access the cell through a row in
the grids Rows collection, I'm happy, as in my other code.  Can anyone
explain this behaviour?



            ExportLineRow newRow = new ExportLineRow();  // Derived from
DataGridViewRow.

            newRow.CreateCells(dgvLayout);

            dgvLayout.Rows.Add(newRow);



            int cx = 0;

            foreach (Column o in newLIne.RecordLayout.Columns.Values)

            {

                int rowIndex = dgvLayout.Rows.Count - 1;

                dgvLayout.Rows[rowIndex].Cells[cx].Value = o.ColumnValue;

                //DataGridViewCell c = newRow.Cells[cx];

                //newRow.Cells[cx].Value = o.ColumnValue;

                cx++;

            }