Copy style from one C1FlexGrid to another?

Posted by: mhamilton on 8 August 2019, 5:48 pm EST

    • Post Options:
    • Link

    Posted 8 August 2019, 5:48 pm EST

    Is it possible to copy the style from one C1FelxGrid to another C1FelxGrid? The example would be the user loads a C1FlexGrid but wants it to look the same (different data) as another C1FlexGrid.

    I tried something like this but it throws an exception that Styles is read only

                
    C1FlexGrid newGrid = new C1FlexGrid();
    newGrid.ReadXml(fileStream);
    newGrid.Styles = oldGrid.Styles;
    
    

    Thanks!

  • Posted 9 August 2019, 1:58 am EST

    Hello Michael,

    This exception occurs because each element of CellStyleCollection is readonly.

    When loading grid from xml document, it loads the data as well as the formatting information.

    Although if you want to just copy the style from one grid to another not the data, you create a XML file of OldGrid using WriteXml method and Load that XML file into another grid using ReadXml method and then change the datasource of the NewGrid.(Style will be retained and data Will be changed). This way all the styling set through Styles property of OldGrid gets copied to the NewGrid.

    Another way of doing this is, you need to use the MergeWith method of CellStyle class and add all the styles one by one which has been set to the OldGrid specifically.

    For example :

    //this copies all the styling info set to the normal cells of the FlexGrid

    newGrid.Styles.Normal.MergeWith(oldGrid.Styles.Normal);

    //this copies all the styling info set to column1 of Flexgrid

    newGrid.Cols[1].Style.MergeWith(oldGrid.Cols[1].Style);

    Thanks and Regards,

    Prabhat Sharma.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels