We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

My formulas do not "adjust" when I drag columns

Is this expected behavior?

If I have a formula in A5 saying "=B5", and I drag B column in front of A column, shouldn't the formula update to "=A5" (since it is not the B column)?

I have the cell with the formula marked as "Enabled=false" if that matters because I do not want someone to click on the cell and change the formula.

thanks







1 Reply

AD Administrator Syncfusion Team May 2, 2008 01:05 PM UTC


Hi Paul,

Thanks for the interest in Syncfusion products.

In a GridControl using DragColumnHeader, the column is removed from one location and inserted in another location, all in one operation. There is no property setting that automatically manages updating the references during this one step move operation.However you could subscribe to the ColsMoved event and update the formulas programmatically. Please refer the code snippet below that shows how we can update the formulas programmatically.


void gridControl1_ColsMoved(object sender, GridRangeMovedEventArgs e)
{
int from = e.From;
int to = e.Target;
char[] oldColumns = new char[gridControl1.ColCount + 1];
char[] newColumns = new char[gridControl1.ColCount + 1];

for (int i = 1; i < gridControl1.ColCount + 1; i++)
{
oldColumns[i] = Convert.ToChar(i + 64);
}
oldColumns.CopyTo(newColumns,0);

if (from != to)
{
#region finding the modified references for the columns
if (from > to)
{
char temp = newColumns[to];
for (int i = to; i < from; i++)
{
newColumns[i] = newColumns[i + 1];
}
newColumns[from] = temp;
}
else if (from < to)
{
char temp = newColumns[to - 1];
for (int i = to - 1; i > from; i--)
{
newColumns[i] = newColumns[i - 1];
}
newColumns[from] = newColumns[to];
newColumns[to] = temp;
}
#endregion
gridControl1.BeginUpdate();
#region Updating the formula
for (int row = 1; row <= gridControl1.RowCount; row++)
{
for (int col = 1; col <= gridControl1.ColCount; col++)
{
GridStyleInfo style = gridControl1[row, col];
if (style.CellType == GridCellTypeName.FormulaCell)
{
GridFormulaTag ftag = style.FormulaTag;
if (ftag != null)
{
string formula = style.CellValue.ToString();
char[] characters = formula.ToCharArray();

MatchCollection mc = Regex.Matches(formula, @"(\w\d)");
foreach (Match m in mc)
{
int index = Array.IndexOf(oldColumns, m.Value[0]);
if (index > 0)
{
characters[m.Index] = newColumns[index];
}
}
string newFormula = new string(characters);
if (formula != newFormula)
{
style.FormulaTag = null;
style.CellValue = newFormula;
}
}
}
}
}
#endregion
#region Recalculating all the formula cells
GridFormulaCellModel model = gridControl1.CellModels["FormulaCell"] as GridFormulaCellModel;
GridFormulaEngine engine = model.Engine;
engine.RecalculateRange(GridRangeInfo.Table());
#endregion
gridControl1.EndUpdate();
gridControl1.Refresh();
}
}



Please refer the sample in the below link that illustrates the above.

http://websamples.syncfusion.com/samples/Grid.Windows/F73214/main.htm


Please let me know if this is not what you needed.

Regards,
Asem.




Loader.
Live Chat Icon For mobile
Up arrow icon