Search code examples
c#.netinfragistics

How to clone an UltraGridRow (Infragistics)?


I would like to clone a UltraGridRow into a new instance of UltraGridRow and change two cells only. Then I would like to add this new UltraGridRow instance to my Band.

I am seeking a way to not have to go through each Cell one by one to copy them into the new instance.

Is there any intelligent and effective way to do this?


Solution

  • The UltraGridRow has a CopyFrom method that should do the trick (documentation). Here's a test for your scenario:

    [Test]
    public void CloneRowCellsTest()
    {
      UltraGridRow objSource = new UltraGridRow();
      objSource.Cells.Add(new UltraGridCell("Original value for cell 0"));
      objSource.Cells.Add(new UltraGridCell("Original value for cell 1"));
    
      UltraGridRow objDestination = new UltraGridRow();
      objDestination.CopyFrom(objSource);
      objDestination.Cells[1].Value = "New value for cell 1";
    
      Assert.AreEqual(objSource.Cells.Count, objDestination.Cells.Count);
      Assert.AreEqual("Original value for cell 0", objDestination.Cells[0].Value);  //Ensure that the value was copied
      Assert.AreEqual("New value for cell 1", objDestination.Cells[1].Value);       //Ensure that the new value was set
      Assert.AreEqual("Original value for cell 1", objSource.Cells[1].Value);       //Ensure that the original was unchanged
    }