Funny DataGridView behaviour - Hours and hours

Brady Kelly <[email protected]> Mon, 28 Jan 2008 22:56:20 +0200
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
From last night I was at work until 03H30 this morning, due to, among other
things, what I find a strange quirk in an unbound DataGridView.  When I use
a class derived from DataGridViewRow, and add two or more of these rows to
the grid, without populating their cells (still creating them), all my
custom properties are set to null from the second row onwards.  It doesn't
matter whether I populate the cells of the first row or not, it always
survives, but for every subsequent row, if I don't populate at least one
cell, it gets sanitised.  Even if the first row has no populated  cells,
populating one cell on a subsequent row ensures its survival.



I will illustrate.  I have numbered only the lines that are relevant.  The
grid does not allow additions, so there are only two rows.



1.       In the current state, firstRow.RowName = "Row A", and
lastRow.RowName = "Row B".

2.       If I comment out line 4, firstRow.RowName = "Row A", and
lastRow.RowName = {null}.

3.       It doesn't seem to matter at all whether lines 1 and 2 are
commented out or not.



    public class NamedRow: DataGridViewRow

    {

        public NamedRow() {}

        public NamedRow(string rowName):this()

        {

            this.RowName = rowName;

        }

        public string RowName { get; set; }

    }



        private void GridBadForm_Load(object sender, EventArgs e)

        {

            dgvLayout.Columns.Add("Column 1", "Column 1");

            dgvLayout.Columns.Add("Column 2", "Column 2");



            NamedRow namedRowA = new NamedRow("Row A");

            namedRowA.CreateCells(dgvLayout);

1           //namedRowA.Cells[0].Value = "A One";

2           //namedRowA.Cells[1].Value = "A Two";



            NamedRow namedRowB = new NamedRow("Row B");

            namedRowB.CreateCells(dgvLayout);

3           //namedRowB.Cells[0].Value = "B One";

4           namedRowB.Cells[1].Value = "B Two";



            dgvLayout.Rows.Clear();

            dgvLayout.Rows.Add(namedRowA);

            dgvLayout.Rows.Add(namedRowB);



            NamedRow firstRow = (NamedRow)dgvLayout.Rows[0];

            NamedRow lastRow = (NamedRow)dgvLayout.Rows[1];

            Debug.Print(firstRow.RowName);

            Debug.Print(lastRow.RowName);

        }