2

I ran into an interesting problem. When I add only one DataGridViewRow to a DataGridView everything works as expected: DataGridView.Rows[0] is the row that was added. When I add a second DataGridviewRow somehow the actual DataGridViewRows are no longer added but clones are added instead. Clones which seem perfectly the same but aren't in fact the same object.

So what I'd like to know is why this is, where this behavior comes from and if possible how to stop the clones from being added instead of the actual rows. I looked over the code for a DataGridViewRowCollection with Reflector but couldn't find anything fishy - but perhaps I missed something.

Here's a code sample that replicates the problem:

DataGridView dgv = new DataGridView { AllowUserToAddRows = false };

DataGridViewTextBoxColumn dgColumn = new DataGridViewTextBoxColumn();
dgv.Columns.Add(dgColumn);

DataGridViewRow drFirst = new DataGridViewRow();
dgv.Rows.Add(drFirst); // Comment this line to showcase the problem

DataGridViewRow drSecond = new DataGridViewRow();
drSecond.Tag = new object();
dgv.Rows.Add(drSecond);

// When drFirst is added this is false - when it isn't this is true (as it should always be?)
bool thisSeemsWrong = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1], drSecond);

// Always true
bool thisSeemsRight = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1].Tag, drSecond.Tag);
2
  • For me it is always false(using VB.NET). Jan 6, 2012 at 10:35
  • That's very strange - I'm definitely not getting false when the line "dgv.Rows.Add(drFirst);" is commented out.
    – TomMhC
    Jan 6, 2012 at 10:57

2 Answers 2

2

The Item property of class DataGridViewRowCollection creates a clone of the GridViewRow and returns that, therefore ReferenceEquals should always be false when there is more than one row.

DataGridViewRow row2 = (DataGridViewRow) dataGridViewRow.Clone();

But if there is only one row and index=0, it returns the same reference.

if (((index == 0) && (this.items.Count() == 1))) {
      dataGridViewRow.IndexInternal = 0;
      dataGridViewRow.StateInternal = this.SharedRowState[0];
      if (((this.DataGridView != null))) {
          this.DataGridView.OnRowUnshared(dataGridViewRow);
      }
      return dataGridViewRow;
}

Here is this complete source of this property(from Reflection):

public DataGridViewRow this[int index]
{
  get
  {
    DataGridViewRow dataGridViewRow = this.SharedRow(index);
    if (dataGridViewRow.Index != -1)
    {
      return dataGridViewRow;
    }
    if ((index == 0) && (this.items.Count == 1))
    {
      dataGridViewRow.IndexInternal = 0;
      dataGridViewRow.StateInternal = this.SharedRowState(0);
      if (this.DataGridView != null)
      {
        this.DataGridView.OnRowUnshared(dataGridViewRow);
      }
      return dataGridViewRow;
    }
    DataGridViewRow row2 = (DataGridViewRow) dataGridViewRow.Clone();
    row2.IndexInternal = index;
    row2.DataGridViewInternal = dataGridViewRow.DataGridView;
    row2.StateInternal = this.SharedRowState(index);
    this.SharedList[index] = row2;
    int num = 0;
    foreach (DataGridViewCell cell in row2.Cells)
    {
      cell.DataGridViewInternal = dataGridViewRow.DataGridView;
      cell.OwningRowInternal = row2;
      cell.OwningColumnInternal = this.DataGridView.Columns[num];
      num++;
    }
    if (row2.HasHeaderCell)
    {
      row2.HeaderCell.DataGridViewInternal = dataGridViewRow.DataGridView;
      row2.HeaderCell.OwningRowInternal = row2;
    }
    if (this.DataGridView != null)
    {
      this.DataGridView.OnRowUnshared(row2);
    }
    return row2;
  }
}

Maybe you'll find information on the why in following article(starting at "Using Shared Rows"):

http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

1
  • Thank you, Tim, that's it! It never occurred to me that this behavior might be situated in the default property. The reason behind this has to do with the principle of shared rows behind the datagridview. Still, an undocumented change in behavior between a grid with one row and a grid with multiple rows is the stuff of doubt-filled lunch breaks. In any case a possible way to get the original row would be to call 'dgv.Rows.SharedRow(index)'.
    – TomMhC
    Jan 6, 2012 at 13:11
0

When you do:

 DataGridViewRow drFirst = new DataGridViewRow();

the index value of this row is -1 in the drFirst object. but when you add it to a Row collection like:

 DataGridViewRow drFirst = new DataGridViewRow();

the row collection gives it a new index depending upon the number of rows already exists in the collection. In your case the index = 0 here.

Similarly for drSecond the index value is -1 when you create it but when you add it to row collection the index value is changed to 1.

so the line:

bool thisSeemsWrong = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1], drSecond);

is comparing two objects with different index values so they will be different so returns false. but this line:

bool thisSeemsRight = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1].Tag, drSecond.Tag);

is just comparing a property Tag which hasn't been changed and returns true.

[UPDATE]

DataGridViewRow drFirst = new DataGridViewRow();
//dgv.Rows.Add(drFirst); //if you comment this line then the thisSeemsWrong is true that's right because it's now comparing the row you add which is drSecond with drSecond

DataGridViewRow drSecond = new DataGridViewRow();
drSecond.Tag = new object();
dgv.Rows.Add(drSecond);

// When drFirst is added this is false - when it isn't this is true (as it should always be?)
bool thisSeemsWrong = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1], drSecond);

DataGridViewRow drFirst = new DataGridViewRow();
dgv.Rows.Add(drFirst); // Now because you have added the first row as drFirst, look what you are comparing against in thisSeemsWrong, you are comparing Row[0] which is drFirst with drSecond which will always be false. You either have to compare Row[1] with drSecond or Row[0] with drFirst??

DataGridViewRow drSecond = new DataGridViewRow();
drSecond.Tag = new object();
dgv.Rows.Add(drSecond);

// When drFirst is added this is false - when it isn't this is true (as it should always be?)
bool thisSeemsWrong = object.ReferenceEquals(dgv.Rows[dgv.Rows.Count - 1], drSecond);
7
  • Does to explain why .NET implicitely creates a copy/clone of the DataGridViewRow(as OP mentioned). DataGridViewRowCollection returned by dgv.Rows is not an Array but a collection, so it's not necessary to create a copy. Apart from that, for me it's always false whether i add the first row as well or not. Jan 6, 2012 at 10:57
  • Thank you for your reply but I already knew about the behavior, namely that a second object, a clone (so indeed with a different index), is created and added. What I'm looking for is why this happens and how to stop it. (edit: this is in reply to Azhar, not Tim's comment - I was too slow)
    – TomMhC
    Jan 6, 2012 at 10:59
  • Well the second object isn't actually a clone of the first as both drFirst and drSecond have different memory allocations. if you do a normal comparison like: bool objCompare = object.ReferenceEquals(new DataGridViewRow(), new DataGridViewRow()); you will get a false because they have different memory allocations. Jan 6, 2012 at 11:20
  • I'm sorry if I'm not being clear enough in my question. Of course drfirst is not the same object as drSecond. The strange thing is that drFirst is not the same object as dgv.Rows[0] either -> it was added as the first object to the collection of rows so it definitely should be the same! This only happens when I add two rows - not when I add just the one.
    – TomMhC
    Jan 6, 2012 at 11:35
  • The way I would approach this is to have a custom class that represents a row in the grid view and have an identifier there called ID of type int. Then compare the ID of two different objects. Jan 6, 2012 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.