Skip to main content Skip to footer

Shift merged ranges in C1FlexGrid

Background:

Every MergedRange have their own column index, so upon adding/removing a column they do not adjust their position accordingly whereas the rest of the columns do. Due to this, MergedRanges overlaps the content of neighboring columns when any column is added or removed.

Steps to Complete:

Access the current column that contains cursor and for each MergedRange in MergedRangeCollection, check whether the current column is to the left of its first column. If yes, then reduce its column index by 1 in case of removal and increase by 1 in case of addition.

int currColIndex = c1FlexGrid1.Col;
CellRangeCollection mergedRangeCollctn = c1FlexGrid1.MergedRanges;

//Column addition 
for (int i = 0; i < mergedRangeCollctn.Count; i++)
{
    if (mergedRangeCollctn[cr].c1 >= currColIndex)
    {
        CellRange mergedRange = mergedRangeCollctn[i];
        mergedRange.c1 += 1;
        mergedRange.c2 += 1;
        mergedRangeCollctn.RemoveAt(i);
        mergedRangeCollctn.Insert(i, mergedRange);
    }
}
c1FlexGrid1.Cols.Insert(c1FlexGrid1.Col);

Ruchir Agarwal